module.c 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938
  1. /* Handle modules, which amounts to loading and saving symbols and
  2. their attendant structures.
  3. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  4. Contributed by Andy Vaught
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* The syntax of gfortran modules resembles that of lisp lists, i.e. a
  18. sequence of atoms, which can be left or right parenthesis, names,
  19. integers or strings. Parenthesis are always matched which allows
  20. us to skip over sections at high speed without having to know
  21. anything about the internal structure of the lists. A "name" is
  22. usually a fortran 95 identifier, but can also start with '@' in
  23. order to reference a hidden symbol.
  24. The first line of a module is an informational message about what
  25. created the module, the file it came from and when it was created.
  26. The second line is a warning for people not to edit the module.
  27. The rest of the module looks like:
  28. ( ( <Interface info for UPLUS> )
  29. ( <Interface info for UMINUS> )
  30. ...
  31. )
  32. ( ( <name of operator interface> <module of op interface> <i/f1> ... )
  33. ...
  34. )
  35. ( ( <name of generic interface> <module of generic interface> <i/f1> ... )
  36. ...
  37. )
  38. ( ( <common name> <symbol> <saved flag>)
  39. ...
  40. )
  41. ( equivalence list )
  42. ( <Symbol Number (in no particular order)>
  43. <True name of symbol>
  44. <Module name of symbol>
  45. ( <symbol information> )
  46. ...
  47. )
  48. ( <Symtree name>
  49. <Ambiguous flag>
  50. <Symbol number>
  51. ...
  52. )
  53. In general, symbols refer to other symbols by their symbol number,
  54. which are zero based. Symbols are written to the module in no
  55. particular order. */
  56. #include "config.h"
  57. #include "system.h"
  58. #include "coretypes.h"
  59. #include "gfortran.h"
  60. #include "arith.h"
  61. #include "match.h"
  62. #include "parse.h" /* FIXME */
  63. #include "constructor.h"
  64. #include "cpp.h"
  65. #include "hash-set.h"
  66. #include "machmode.h"
  67. #include "vec.h"
  68. #include "double-int.h"
  69. #include "input.h"
  70. #include "alias.h"
  71. #include "symtab.h"
  72. #include "options.h"
  73. #include "wide-int.h"
  74. #include "inchash.h"
  75. #include "tree.h"
  76. #include "stringpool.h"
  77. #include "scanner.h"
  78. #include <zlib.h>
  79. #define MODULE_EXTENSION ".mod"
  80. /* Don't put any single quote (') in MOD_VERSION, if you want it to be
  81. recognized. */
  82. #define MOD_VERSION "14"
  83. /* Structure that describes a position within a module file. */
  84. typedef struct
  85. {
  86. int column, line;
  87. long pos;
  88. }
  89. module_locus;
  90. /* Structure for list of symbols of intrinsic modules. */
  91. typedef struct
  92. {
  93. int id;
  94. const char *name;
  95. int value;
  96. int standard;
  97. }
  98. intmod_sym;
  99. typedef enum
  100. {
  101. P_UNKNOWN = 0, P_OTHER, P_NAMESPACE, P_COMPONENT, P_SYMBOL
  102. }
  103. pointer_t;
  104. /* The fixup structure lists pointers to pointers that have to
  105. be updated when a pointer value becomes known. */
  106. typedef struct fixup_t
  107. {
  108. void **pointer;
  109. struct fixup_t *next;
  110. }
  111. fixup_t;
  112. /* Structure for holding extra info needed for pointers being read. */
  113. enum gfc_rsym_state
  114. {
  115. UNUSED,
  116. NEEDED,
  117. USED
  118. };
  119. enum gfc_wsym_state
  120. {
  121. UNREFERENCED = 0,
  122. NEEDS_WRITE,
  123. WRITTEN
  124. };
  125. typedef struct pointer_info
  126. {
  127. BBT_HEADER (pointer_info);
  128. int integer;
  129. pointer_t type;
  130. /* The first component of each member of the union is the pointer
  131. being stored. */
  132. fixup_t *fixup;
  133. union
  134. {
  135. void *pointer; /* Member for doing pointer searches. */
  136. struct
  137. {
  138. gfc_symbol *sym;
  139. char *true_name, *module, *binding_label;
  140. fixup_t *stfixup;
  141. gfc_symtree *symtree;
  142. enum gfc_rsym_state state;
  143. int ns, referenced, renamed;
  144. module_locus where;
  145. }
  146. rsym;
  147. struct
  148. {
  149. gfc_symbol *sym;
  150. enum gfc_wsym_state state;
  151. }
  152. wsym;
  153. }
  154. u;
  155. }
  156. pointer_info;
  157. #define gfc_get_pointer_info() XCNEW (pointer_info)
  158. /* Local variables */
  159. /* The gzFile for the module we're reading or writing. */
  160. static gzFile module_fp;
  161. /* The name of the module we're reading (USE'ing) or writing. */
  162. static const char *module_name;
  163. static gfc_use_list *module_list;
  164. /* If we're reading an intrinsic module, this is its ID. */
  165. static intmod_id current_intmod;
  166. /* Content of module. */
  167. static char* module_content;
  168. static long module_pos;
  169. static int module_line, module_column, only_flag;
  170. static int prev_module_line, prev_module_column;
  171. static enum
  172. { IO_INPUT, IO_OUTPUT }
  173. iomode;
  174. static gfc_use_rename *gfc_rename_list;
  175. static pointer_info *pi_root;
  176. static int symbol_number; /* Counter for assigning symbol numbers */
  177. /* Tells mio_expr_ref to make symbols for unused equivalence members. */
  178. static bool in_load_equiv;
  179. /*****************************************************************/
  180. /* Pointer/integer conversion. Pointers between structures are stored
  181. as integers in the module file. The next couple of subroutines
  182. handle this translation for reading and writing. */
  183. /* Recursively free the tree of pointer structures. */
  184. static void
  185. free_pi_tree (pointer_info *p)
  186. {
  187. if (p == NULL)
  188. return;
  189. if (p->fixup != NULL)
  190. gfc_internal_error ("free_pi_tree(): Unresolved fixup");
  191. free_pi_tree (p->left);
  192. free_pi_tree (p->right);
  193. if (iomode == IO_INPUT)
  194. {
  195. XDELETEVEC (p->u.rsym.true_name);
  196. XDELETEVEC (p->u.rsym.module);
  197. XDELETEVEC (p->u.rsym.binding_label);
  198. }
  199. free (p);
  200. }
  201. /* Compare pointers when searching by pointer. Used when writing a
  202. module. */
  203. static int
  204. compare_pointers (void *_sn1, void *_sn2)
  205. {
  206. pointer_info *sn1, *sn2;
  207. sn1 = (pointer_info *) _sn1;
  208. sn2 = (pointer_info *) _sn2;
  209. if (sn1->u.pointer < sn2->u.pointer)
  210. return -1;
  211. if (sn1->u.pointer > sn2->u.pointer)
  212. return 1;
  213. return 0;
  214. }
  215. /* Compare integers when searching by integer. Used when reading a
  216. module. */
  217. static int
  218. compare_integers (void *_sn1, void *_sn2)
  219. {
  220. pointer_info *sn1, *sn2;
  221. sn1 = (pointer_info *) _sn1;
  222. sn2 = (pointer_info *) _sn2;
  223. if (sn1->integer < sn2->integer)
  224. return -1;
  225. if (sn1->integer > sn2->integer)
  226. return 1;
  227. return 0;
  228. }
  229. /* Initialize the pointer_info tree. */
  230. static void
  231. init_pi_tree (void)
  232. {
  233. compare_fn compare;
  234. pointer_info *p;
  235. pi_root = NULL;
  236. compare = (iomode == IO_INPUT) ? compare_integers : compare_pointers;
  237. /* Pointer 0 is the NULL pointer. */
  238. p = gfc_get_pointer_info ();
  239. p->u.pointer = NULL;
  240. p->integer = 0;
  241. p->type = P_OTHER;
  242. gfc_insert_bbt (&pi_root, p, compare);
  243. /* Pointer 1 is the current namespace. */
  244. p = gfc_get_pointer_info ();
  245. p->u.pointer = gfc_current_ns;
  246. p->integer = 1;
  247. p->type = P_NAMESPACE;
  248. gfc_insert_bbt (&pi_root, p, compare);
  249. symbol_number = 2;
  250. }
  251. /* During module writing, call here with a pointer to something,
  252. returning the pointer_info node. */
  253. static pointer_info *
  254. find_pointer (void *gp)
  255. {
  256. pointer_info *p;
  257. p = pi_root;
  258. while (p != NULL)
  259. {
  260. if (p->u.pointer == gp)
  261. break;
  262. p = (gp < p->u.pointer) ? p->left : p->right;
  263. }
  264. return p;
  265. }
  266. /* Given a pointer while writing, returns the pointer_info tree node,
  267. creating it if it doesn't exist. */
  268. static pointer_info *
  269. get_pointer (void *gp)
  270. {
  271. pointer_info *p;
  272. p = find_pointer (gp);
  273. if (p != NULL)
  274. return p;
  275. /* Pointer doesn't have an integer. Give it one. */
  276. p = gfc_get_pointer_info ();
  277. p->u.pointer = gp;
  278. p->integer = symbol_number++;
  279. gfc_insert_bbt (&pi_root, p, compare_pointers);
  280. return p;
  281. }
  282. /* Given an integer during reading, find it in the pointer_info tree,
  283. creating the node if not found. */
  284. static pointer_info *
  285. get_integer (int integer)
  286. {
  287. pointer_info *p, t;
  288. int c;
  289. t.integer = integer;
  290. p = pi_root;
  291. while (p != NULL)
  292. {
  293. c = compare_integers (&t, p);
  294. if (c == 0)
  295. break;
  296. p = (c < 0) ? p->left : p->right;
  297. }
  298. if (p != NULL)
  299. return p;
  300. p = gfc_get_pointer_info ();
  301. p->integer = integer;
  302. p->u.pointer = NULL;
  303. gfc_insert_bbt (&pi_root, p, compare_integers);
  304. return p;
  305. }
  306. /* Resolve any fixups using a known pointer. */
  307. static void
  308. resolve_fixups (fixup_t *f, void *gp)
  309. {
  310. fixup_t *next;
  311. for (; f; f = next)
  312. {
  313. next = f->next;
  314. *(f->pointer) = gp;
  315. free (f);
  316. }
  317. }
  318. /* Convert a string such that it starts with a lower-case character. Used
  319. to convert the symtree name of a derived-type to the symbol name or to
  320. the name of the associated generic function. */
  321. static const char *
  322. dt_lower_string (const char *name)
  323. {
  324. if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
  325. return gfc_get_string ("%c%s", (char) TOLOWER ((unsigned char) name[0]),
  326. &name[1]);
  327. return gfc_get_string (name);
  328. }
  329. /* Convert a string such that it starts with an upper-case character. Used to
  330. return the symtree-name for a derived type; the symbol name itself and the
  331. symtree/symbol name of the associated generic function start with a lower-
  332. case character. */
  333. static const char *
  334. dt_upper_string (const char *name)
  335. {
  336. if (name[0] != (char) TOUPPER ((unsigned char) name[0]))
  337. return gfc_get_string ("%c%s", (char) TOUPPER ((unsigned char) name[0]),
  338. &name[1]);
  339. return gfc_get_string (name);
  340. }
  341. /* Call here during module reading when we know what pointer to
  342. associate with an integer. Any fixups that exist are resolved at
  343. this time. */
  344. static void
  345. associate_integer_pointer (pointer_info *p, void *gp)
  346. {
  347. if (p->u.pointer != NULL)
  348. gfc_internal_error ("associate_integer_pointer(): Already associated");
  349. p->u.pointer = gp;
  350. resolve_fixups (p->fixup, gp);
  351. p->fixup = NULL;
  352. }
  353. /* During module reading, given an integer and a pointer to a pointer,
  354. either store the pointer from an already-known value or create a
  355. fixup structure in order to store things later. Returns zero if
  356. the reference has been actually stored, or nonzero if the reference
  357. must be fixed later (i.e., associate_integer_pointer must be called
  358. sometime later. Returns the pointer_info structure. */
  359. static pointer_info *
  360. add_fixup (int integer, void *gp)
  361. {
  362. pointer_info *p;
  363. fixup_t *f;
  364. char **cp;
  365. p = get_integer (integer);
  366. if (p->integer == 0 || p->u.pointer != NULL)
  367. {
  368. cp = (char **) gp;
  369. *cp = (char *) p->u.pointer;
  370. }
  371. else
  372. {
  373. f = XCNEW (fixup_t);
  374. f->next = p->fixup;
  375. p->fixup = f;
  376. f->pointer = (void **) gp;
  377. }
  378. return p;
  379. }
  380. /*****************************************************************/
  381. /* Parser related subroutines */
  382. /* Free the rename list left behind by a USE statement. */
  383. static void
  384. free_rename (gfc_use_rename *list)
  385. {
  386. gfc_use_rename *next;
  387. for (; list; list = next)
  388. {
  389. next = list->next;
  390. free (list);
  391. }
  392. }
  393. /* Match a USE statement. */
  394. match
  395. gfc_match_use (void)
  396. {
  397. char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
  398. gfc_use_rename *tail = NULL, *new_use;
  399. interface_type type, type2;
  400. gfc_intrinsic_op op;
  401. match m;
  402. gfc_use_list *use_list;
  403. use_list = gfc_get_use_list ();
  404. if (gfc_match (" , ") == MATCH_YES)
  405. {
  406. if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
  407. {
  408. if (!gfc_notify_std (GFC_STD_F2003, "module "
  409. "nature in USE statement at %C"))
  410. goto cleanup;
  411. if (strcmp (module_nature, "intrinsic") == 0)
  412. use_list->intrinsic = true;
  413. else
  414. {
  415. if (strcmp (module_nature, "non_intrinsic") == 0)
  416. use_list->non_intrinsic = true;
  417. else
  418. {
  419. gfc_error ("Module nature in USE statement at %C shall "
  420. "be either INTRINSIC or NON_INTRINSIC");
  421. goto cleanup;
  422. }
  423. }
  424. }
  425. else
  426. {
  427. /* Help output a better error message than "Unclassifiable
  428. statement". */
  429. gfc_match (" %n", module_nature);
  430. if (strcmp (module_nature, "intrinsic") == 0
  431. || strcmp (module_nature, "non_intrinsic") == 0)
  432. gfc_error ("\"::\" was expected after module nature at %C "
  433. "but was not found");
  434. free (use_list);
  435. return m;
  436. }
  437. }
  438. else
  439. {
  440. m = gfc_match (" ::");
  441. if (m == MATCH_YES &&
  442. !gfc_notify_std(GFC_STD_F2003, "\"USE :: module\" at %C"))
  443. goto cleanup;
  444. if (m != MATCH_YES)
  445. {
  446. m = gfc_match ("% ");
  447. if (m != MATCH_YES)
  448. {
  449. free (use_list);
  450. return m;
  451. }
  452. }
  453. }
  454. use_list->where = gfc_current_locus;
  455. m = gfc_match_name (name);
  456. if (m != MATCH_YES)
  457. {
  458. free (use_list);
  459. return m;
  460. }
  461. use_list->module_name = gfc_get_string (name);
  462. if (gfc_match_eos () == MATCH_YES)
  463. goto done;
  464. if (gfc_match_char (',') != MATCH_YES)
  465. goto syntax;
  466. if (gfc_match (" only :") == MATCH_YES)
  467. use_list->only_flag = true;
  468. if (gfc_match_eos () == MATCH_YES)
  469. goto done;
  470. for (;;)
  471. {
  472. /* Get a new rename struct and add it to the rename list. */
  473. new_use = gfc_get_use_rename ();
  474. new_use->where = gfc_current_locus;
  475. new_use->found = 0;
  476. if (use_list->rename == NULL)
  477. use_list->rename = new_use;
  478. else
  479. tail->next = new_use;
  480. tail = new_use;
  481. /* See what kind of interface we're dealing with. Assume it is
  482. not an operator. */
  483. new_use->op = INTRINSIC_NONE;
  484. if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
  485. goto cleanup;
  486. switch (type)
  487. {
  488. case INTERFACE_NAMELESS:
  489. gfc_error ("Missing generic specification in USE statement at %C");
  490. goto cleanup;
  491. case INTERFACE_USER_OP:
  492. case INTERFACE_GENERIC:
  493. m = gfc_match (" =>");
  494. if (type == INTERFACE_USER_OP && m == MATCH_YES
  495. && (!gfc_notify_std(GFC_STD_F2003, "Renaming "
  496. "operators in USE statements at %C")))
  497. goto cleanup;
  498. if (type == INTERFACE_USER_OP)
  499. new_use->op = INTRINSIC_USER;
  500. if (use_list->only_flag)
  501. {
  502. if (m != MATCH_YES)
  503. strcpy (new_use->use_name, name);
  504. else
  505. {
  506. strcpy (new_use->local_name, name);
  507. m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
  508. if (type != type2)
  509. goto syntax;
  510. if (m == MATCH_NO)
  511. goto syntax;
  512. if (m == MATCH_ERROR)
  513. goto cleanup;
  514. }
  515. }
  516. else
  517. {
  518. if (m != MATCH_YES)
  519. goto syntax;
  520. strcpy (new_use->local_name, name);
  521. m = gfc_match_generic_spec (&type2, new_use->use_name, &op);
  522. if (type != type2)
  523. goto syntax;
  524. if (m == MATCH_NO)
  525. goto syntax;
  526. if (m == MATCH_ERROR)
  527. goto cleanup;
  528. }
  529. if (strcmp (new_use->use_name, use_list->module_name) == 0
  530. || strcmp (new_use->local_name, use_list->module_name) == 0)
  531. {
  532. gfc_error ("The name %qs at %C has already been used as "
  533. "an external module name.", use_list->module_name);
  534. goto cleanup;
  535. }
  536. break;
  537. case INTERFACE_INTRINSIC_OP:
  538. new_use->op = op;
  539. break;
  540. default:
  541. gcc_unreachable ();
  542. }
  543. if (gfc_match_eos () == MATCH_YES)
  544. break;
  545. if (gfc_match_char (',') != MATCH_YES)
  546. goto syntax;
  547. }
  548. done:
  549. if (module_list)
  550. {
  551. gfc_use_list *last = module_list;
  552. while (last->next)
  553. last = last->next;
  554. last->next = use_list;
  555. }
  556. else
  557. module_list = use_list;
  558. return MATCH_YES;
  559. syntax:
  560. gfc_syntax_error (ST_USE);
  561. cleanup:
  562. free_rename (use_list->rename);
  563. free (use_list);
  564. return MATCH_ERROR;
  565. }
  566. /* Given a name and a number, inst, return the inst name
  567. under which to load this symbol. Returns NULL if this
  568. symbol shouldn't be loaded. If inst is zero, returns
  569. the number of instances of this name. If interface is
  570. true, a user-defined operator is sought, otherwise only
  571. non-operators are sought. */
  572. static const char *
  573. find_use_name_n (const char *name, int *inst, bool interface)
  574. {
  575. gfc_use_rename *u;
  576. const char *low_name = NULL;
  577. int i;
  578. /* For derived types. */
  579. if (name[0] != (char) TOLOWER ((unsigned char) name[0]))
  580. low_name = dt_lower_string (name);
  581. i = 0;
  582. for (u = gfc_rename_list; u; u = u->next)
  583. {
  584. if ((!low_name && strcmp (u->use_name, name) != 0)
  585. || (low_name && strcmp (u->use_name, low_name) != 0)
  586. || (u->op == INTRINSIC_USER && !interface)
  587. || (u->op != INTRINSIC_USER && interface))
  588. continue;
  589. if (++i == *inst)
  590. break;
  591. }
  592. if (!*inst)
  593. {
  594. *inst = i;
  595. return NULL;
  596. }
  597. if (u == NULL)
  598. return only_flag ? NULL : name;
  599. u->found = 1;
  600. if (low_name)
  601. {
  602. if (u->local_name[0] == '\0')
  603. return name;
  604. return dt_upper_string (u->local_name);
  605. }
  606. return (u->local_name[0] != '\0') ? u->local_name : name;
  607. }
  608. /* Given a name, return the name under which to load this symbol.
  609. Returns NULL if this symbol shouldn't be loaded. */
  610. static const char *
  611. find_use_name (const char *name, bool interface)
  612. {
  613. int i = 1;
  614. return find_use_name_n (name, &i, interface);
  615. }
  616. /* Given a real name, return the number of use names associated with it. */
  617. static int
  618. number_use_names (const char *name, bool interface)
  619. {
  620. int i = 0;
  621. find_use_name_n (name, &i, interface);
  622. return i;
  623. }
  624. /* Try to find the operator in the current list. */
  625. static gfc_use_rename *
  626. find_use_operator (gfc_intrinsic_op op)
  627. {
  628. gfc_use_rename *u;
  629. for (u = gfc_rename_list; u; u = u->next)
  630. if (u->op == op)
  631. return u;
  632. return NULL;
  633. }
  634. /*****************************************************************/
  635. /* The next couple of subroutines maintain a tree used to avoid a
  636. brute-force search for a combination of true name and module name.
  637. While symtree names, the name that a particular symbol is known by
  638. can changed with USE statements, we still have to keep track of the
  639. true names to generate the correct reference, and also avoid
  640. loading the same real symbol twice in a program unit.
  641. When we start reading, the true name tree is built and maintained
  642. as symbols are read. The tree is searched as we load new symbols
  643. to see if it already exists someplace in the namespace. */
  644. typedef struct true_name
  645. {
  646. BBT_HEADER (true_name);
  647. const char *name;
  648. gfc_symbol *sym;
  649. }
  650. true_name;
  651. static true_name *true_name_root;
  652. /* Compare two true_name structures. */
  653. static int
  654. compare_true_names (void *_t1, void *_t2)
  655. {
  656. true_name *t1, *t2;
  657. int c;
  658. t1 = (true_name *) _t1;
  659. t2 = (true_name *) _t2;
  660. c = ((t1->sym->module > t2->sym->module)
  661. - (t1->sym->module < t2->sym->module));
  662. if (c != 0)
  663. return c;
  664. return strcmp (t1->name, t2->name);
  665. }
  666. /* Given a true name, search the true name tree to see if it exists
  667. within the main namespace. */
  668. static gfc_symbol *
  669. find_true_name (const char *name, const char *module)
  670. {
  671. true_name t, *p;
  672. gfc_symbol sym;
  673. int c;
  674. t.name = gfc_get_string (name);
  675. if (module != NULL)
  676. sym.module = gfc_get_string (module);
  677. else
  678. sym.module = NULL;
  679. t.sym = &sym;
  680. p = true_name_root;
  681. while (p != NULL)
  682. {
  683. c = compare_true_names ((void *) (&t), (void *) p);
  684. if (c == 0)
  685. return p->sym;
  686. p = (c < 0) ? p->left : p->right;
  687. }
  688. return NULL;
  689. }
  690. /* Given a gfc_symbol pointer that is not in the true name tree, add it. */
  691. static void
  692. add_true_name (gfc_symbol *sym)
  693. {
  694. true_name *t;
  695. t = XCNEW (true_name);
  696. t->sym = sym;
  697. if (sym->attr.flavor == FL_DERIVED)
  698. t->name = dt_upper_string (sym->name);
  699. else
  700. t->name = sym->name;
  701. gfc_insert_bbt (&true_name_root, t, compare_true_names);
  702. }
  703. /* Recursive function to build the initial true name tree by
  704. recursively traversing the current namespace. */
  705. static void
  706. build_tnt (gfc_symtree *st)
  707. {
  708. const char *name;
  709. if (st == NULL)
  710. return;
  711. build_tnt (st->left);
  712. build_tnt (st->right);
  713. if (st->n.sym->attr.flavor == FL_DERIVED)
  714. name = dt_upper_string (st->n.sym->name);
  715. else
  716. name = st->n.sym->name;
  717. if (find_true_name (name, st->n.sym->module) != NULL)
  718. return;
  719. add_true_name (st->n.sym);
  720. }
  721. /* Initialize the true name tree with the current namespace. */
  722. static void
  723. init_true_name_tree (void)
  724. {
  725. true_name_root = NULL;
  726. build_tnt (gfc_current_ns->sym_root);
  727. }
  728. /* Recursively free a true name tree node. */
  729. static void
  730. free_true_name (true_name *t)
  731. {
  732. if (t == NULL)
  733. return;
  734. free_true_name (t->left);
  735. free_true_name (t->right);
  736. free (t);
  737. }
  738. /*****************************************************************/
  739. /* Module reading and writing. */
  740. /* The following are versions similar to the ones in scanner.c, but
  741. for dealing with compressed module files. */
  742. static gzFile
  743. gzopen_included_file_1 (const char *name, gfc_directorylist *list,
  744. bool module, bool system)
  745. {
  746. char *fullname;
  747. gfc_directorylist *p;
  748. gzFile f;
  749. for (p = list; p; p = p->next)
  750. {
  751. if (module && !p->use_for_modules)
  752. continue;
  753. fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
  754. strcpy (fullname, p->path);
  755. strcat (fullname, name);
  756. f = gzopen (fullname, "r");
  757. if (f != NULL)
  758. {
  759. if (gfc_cpp_makedep ())
  760. gfc_cpp_add_dep (fullname, system);
  761. return f;
  762. }
  763. }
  764. return NULL;
  765. }
  766. static gzFile
  767. gzopen_included_file (const char *name, bool include_cwd, bool module)
  768. {
  769. gzFile f = NULL;
  770. if (IS_ABSOLUTE_PATH (name) || include_cwd)
  771. {
  772. f = gzopen (name, "r");
  773. if (f && gfc_cpp_makedep ())
  774. gfc_cpp_add_dep (name, false);
  775. }
  776. if (!f)
  777. f = gzopen_included_file_1 (name, include_dirs, module, false);
  778. return f;
  779. }
  780. static gzFile
  781. gzopen_intrinsic_module (const char* name)
  782. {
  783. gzFile f = NULL;
  784. if (IS_ABSOLUTE_PATH (name))
  785. {
  786. f = gzopen (name, "r");
  787. if (f && gfc_cpp_makedep ())
  788. gfc_cpp_add_dep (name, true);
  789. }
  790. if (!f)
  791. f = gzopen_included_file_1 (name, intrinsic_modules_dirs, true, true);
  792. return f;
  793. }
  794. typedef enum
  795. {
  796. ATOM_NAME, ATOM_LPAREN, ATOM_RPAREN, ATOM_INTEGER, ATOM_STRING
  797. }
  798. atom_type;
  799. static atom_type last_atom;
  800. /* The name buffer must be at least as long as a symbol name. Right
  801. now it's not clear how we're going to store numeric constants--
  802. probably as a hexadecimal string, since this will allow the exact
  803. number to be preserved (this can't be done by a decimal
  804. representation). Worry about that later. TODO! */
  805. #define MAX_ATOM_SIZE 100
  806. static int atom_int;
  807. static char *atom_string, atom_name[MAX_ATOM_SIZE];
  808. /* Report problems with a module. Error reporting is not very
  809. elaborate, since this sorts of errors shouldn't really happen.
  810. This subroutine never returns. */
  811. static void bad_module (const char *) ATTRIBUTE_NORETURN;
  812. static void
  813. bad_module (const char *msgid)
  814. {
  815. XDELETEVEC (module_content);
  816. module_content = NULL;
  817. switch (iomode)
  818. {
  819. case IO_INPUT:
  820. gfc_fatal_error ("Reading module %qs at line %d column %d: %s",
  821. module_name, module_line, module_column, msgid);
  822. break;
  823. case IO_OUTPUT:
  824. gfc_fatal_error ("Writing module %qs at line %d column %d: %s",
  825. module_name, module_line, module_column, msgid);
  826. break;
  827. default:
  828. gfc_fatal_error ("Module %qs at line %d column %d: %s",
  829. module_name, module_line, module_column, msgid);
  830. break;
  831. }
  832. }
  833. /* Set the module's input pointer. */
  834. static void
  835. set_module_locus (module_locus *m)
  836. {
  837. module_column = m->column;
  838. module_line = m->line;
  839. module_pos = m->pos;
  840. }
  841. /* Get the module's input pointer so that we can restore it later. */
  842. static void
  843. get_module_locus (module_locus *m)
  844. {
  845. m->column = module_column;
  846. m->line = module_line;
  847. m->pos = module_pos;
  848. }
  849. /* Get the next character in the module, updating our reckoning of
  850. where we are. */
  851. static int
  852. module_char (void)
  853. {
  854. const char c = module_content[module_pos++];
  855. if (c == '\0')
  856. bad_module ("Unexpected EOF");
  857. prev_module_line = module_line;
  858. prev_module_column = module_column;
  859. if (c == '\n')
  860. {
  861. module_line++;
  862. module_column = 0;
  863. }
  864. module_column++;
  865. return c;
  866. }
  867. /* Unget a character while remembering the line and column. Works for
  868. a single character only. */
  869. static void
  870. module_unget_char (void)
  871. {
  872. module_line = prev_module_line;
  873. module_column = prev_module_column;
  874. module_pos--;
  875. }
  876. /* Parse a string constant. The delimiter is guaranteed to be a
  877. single quote. */
  878. static void
  879. parse_string (void)
  880. {
  881. int c;
  882. size_t cursz = 30;
  883. size_t len = 0;
  884. atom_string = XNEWVEC (char, cursz);
  885. for ( ; ; )
  886. {
  887. c = module_char ();
  888. if (c == '\'')
  889. {
  890. int c2 = module_char ();
  891. if (c2 != '\'')
  892. {
  893. module_unget_char ();
  894. break;
  895. }
  896. }
  897. if (len >= cursz)
  898. {
  899. cursz *= 2;
  900. atom_string = XRESIZEVEC (char, atom_string, cursz);
  901. }
  902. atom_string[len] = c;
  903. len++;
  904. }
  905. atom_string = XRESIZEVEC (char, atom_string, len + 1);
  906. atom_string[len] = '\0'; /* C-style string for debug purposes. */
  907. }
  908. /* Parse a small integer. */
  909. static void
  910. parse_integer (int c)
  911. {
  912. atom_int = c - '0';
  913. for (;;)
  914. {
  915. c = module_char ();
  916. if (!ISDIGIT (c))
  917. {
  918. module_unget_char ();
  919. break;
  920. }
  921. atom_int = 10 * atom_int + c - '0';
  922. if (atom_int > 99999999)
  923. bad_module ("Integer overflow");
  924. }
  925. }
  926. /* Parse a name. */
  927. static void
  928. parse_name (int c)
  929. {
  930. char *p;
  931. int len;
  932. p = atom_name;
  933. *p++ = c;
  934. len = 1;
  935. for (;;)
  936. {
  937. c = module_char ();
  938. if (!ISALNUM (c) && c != '_' && c != '-')
  939. {
  940. module_unget_char ();
  941. break;
  942. }
  943. *p++ = c;
  944. if (++len > GFC_MAX_SYMBOL_LEN)
  945. bad_module ("Name too long");
  946. }
  947. *p = '\0';
  948. }
  949. /* Read the next atom in the module's input stream. */
  950. static atom_type
  951. parse_atom (void)
  952. {
  953. int c;
  954. do
  955. {
  956. c = module_char ();
  957. }
  958. while (c == ' ' || c == '\r' || c == '\n');
  959. switch (c)
  960. {
  961. case '(':
  962. return ATOM_LPAREN;
  963. case ')':
  964. return ATOM_RPAREN;
  965. case '\'':
  966. parse_string ();
  967. return ATOM_STRING;
  968. case '0':
  969. case '1':
  970. case '2':
  971. case '3':
  972. case '4':
  973. case '5':
  974. case '6':
  975. case '7':
  976. case '8':
  977. case '9':
  978. parse_integer (c);
  979. return ATOM_INTEGER;
  980. case 'a':
  981. case 'b':
  982. case 'c':
  983. case 'd':
  984. case 'e':
  985. case 'f':
  986. case 'g':
  987. case 'h':
  988. case 'i':
  989. case 'j':
  990. case 'k':
  991. case 'l':
  992. case 'm':
  993. case 'n':
  994. case 'o':
  995. case 'p':
  996. case 'q':
  997. case 'r':
  998. case 's':
  999. case 't':
  1000. case 'u':
  1001. case 'v':
  1002. case 'w':
  1003. case 'x':
  1004. case 'y':
  1005. case 'z':
  1006. case 'A':
  1007. case 'B':
  1008. case 'C':
  1009. case 'D':
  1010. case 'E':
  1011. case 'F':
  1012. case 'G':
  1013. case 'H':
  1014. case 'I':
  1015. case 'J':
  1016. case 'K':
  1017. case 'L':
  1018. case 'M':
  1019. case 'N':
  1020. case 'O':
  1021. case 'P':
  1022. case 'Q':
  1023. case 'R':
  1024. case 'S':
  1025. case 'T':
  1026. case 'U':
  1027. case 'V':
  1028. case 'W':
  1029. case 'X':
  1030. case 'Y':
  1031. case 'Z':
  1032. parse_name (c);
  1033. return ATOM_NAME;
  1034. default:
  1035. bad_module ("Bad name");
  1036. }
  1037. /* Not reached. */
  1038. }
  1039. /* Peek at the next atom on the input. */
  1040. static atom_type
  1041. peek_atom (void)
  1042. {
  1043. int c;
  1044. do
  1045. {
  1046. c = module_char ();
  1047. }
  1048. while (c == ' ' || c == '\r' || c == '\n');
  1049. switch (c)
  1050. {
  1051. case '(':
  1052. module_unget_char ();
  1053. return ATOM_LPAREN;
  1054. case ')':
  1055. module_unget_char ();
  1056. return ATOM_RPAREN;
  1057. case '\'':
  1058. module_unget_char ();
  1059. return ATOM_STRING;
  1060. case '0':
  1061. case '1':
  1062. case '2':
  1063. case '3':
  1064. case '4':
  1065. case '5':
  1066. case '6':
  1067. case '7':
  1068. case '8':
  1069. case '9':
  1070. module_unget_char ();
  1071. return ATOM_INTEGER;
  1072. case 'a':
  1073. case 'b':
  1074. case 'c':
  1075. case 'd':
  1076. case 'e':
  1077. case 'f':
  1078. case 'g':
  1079. case 'h':
  1080. case 'i':
  1081. case 'j':
  1082. case 'k':
  1083. case 'l':
  1084. case 'm':
  1085. case 'n':
  1086. case 'o':
  1087. case 'p':
  1088. case 'q':
  1089. case 'r':
  1090. case 's':
  1091. case 't':
  1092. case 'u':
  1093. case 'v':
  1094. case 'w':
  1095. case 'x':
  1096. case 'y':
  1097. case 'z':
  1098. case 'A':
  1099. case 'B':
  1100. case 'C':
  1101. case 'D':
  1102. case 'E':
  1103. case 'F':
  1104. case 'G':
  1105. case 'H':
  1106. case 'I':
  1107. case 'J':
  1108. case 'K':
  1109. case 'L':
  1110. case 'M':
  1111. case 'N':
  1112. case 'O':
  1113. case 'P':
  1114. case 'Q':
  1115. case 'R':
  1116. case 'S':
  1117. case 'T':
  1118. case 'U':
  1119. case 'V':
  1120. case 'W':
  1121. case 'X':
  1122. case 'Y':
  1123. case 'Z':
  1124. module_unget_char ();
  1125. return ATOM_NAME;
  1126. default:
  1127. bad_module ("Bad name");
  1128. }
  1129. }
  1130. /* Read the next atom from the input, requiring that it be a
  1131. particular kind. */
  1132. static void
  1133. require_atom (atom_type type)
  1134. {
  1135. atom_type t;
  1136. const char *p;
  1137. int column, line;
  1138. column = module_column;
  1139. line = module_line;
  1140. t = parse_atom ();
  1141. if (t != type)
  1142. {
  1143. switch (type)
  1144. {
  1145. case ATOM_NAME:
  1146. p = _("Expected name");
  1147. break;
  1148. case ATOM_LPAREN:
  1149. p = _("Expected left parenthesis");
  1150. break;
  1151. case ATOM_RPAREN:
  1152. p = _("Expected right parenthesis");
  1153. break;
  1154. case ATOM_INTEGER:
  1155. p = _("Expected integer");
  1156. break;
  1157. case ATOM_STRING:
  1158. p = _("Expected string");
  1159. break;
  1160. default:
  1161. gfc_internal_error ("require_atom(): bad atom type required");
  1162. }
  1163. module_column = column;
  1164. module_line = line;
  1165. bad_module (p);
  1166. }
  1167. }
  1168. /* Given a pointer to an mstring array, require that the current input
  1169. be one of the strings in the array. We return the enum value. */
  1170. static int
  1171. find_enum (const mstring *m)
  1172. {
  1173. int i;
  1174. i = gfc_string2code (m, atom_name);
  1175. if (i >= 0)
  1176. return i;
  1177. bad_module ("find_enum(): Enum not found");
  1178. /* Not reached. */
  1179. }
  1180. /* Read a string. The caller is responsible for freeing. */
  1181. static char*
  1182. read_string (void)
  1183. {
  1184. char* p;
  1185. require_atom (ATOM_STRING);
  1186. p = atom_string;
  1187. atom_string = NULL;
  1188. return p;
  1189. }
  1190. /**************** Module output subroutines ***************************/
  1191. /* Output a character to a module file. */
  1192. static void
  1193. write_char (char out)
  1194. {
  1195. if (gzputc (module_fp, out) == EOF)
  1196. gfc_fatal_error ("Error writing modules file: %s", xstrerror (errno));
  1197. if (out != '\n')
  1198. module_column++;
  1199. else
  1200. {
  1201. module_column = 1;
  1202. module_line++;
  1203. }
  1204. }
  1205. /* Write an atom to a module. The line wrapping isn't perfect, but it
  1206. should work most of the time. This isn't that big of a deal, since
  1207. the file really isn't meant to be read by people anyway. */
  1208. static void
  1209. write_atom (atom_type atom, const void *v)
  1210. {
  1211. char buffer[20];
  1212. /* Workaround -Wmaybe-uninitialized false positive during
  1213. profiledbootstrap by initializing them. */
  1214. int i = 0, len;
  1215. const char *p;
  1216. switch (atom)
  1217. {
  1218. case ATOM_STRING:
  1219. case ATOM_NAME:
  1220. p = (const char *) v;
  1221. break;
  1222. case ATOM_LPAREN:
  1223. p = "(";
  1224. break;
  1225. case ATOM_RPAREN:
  1226. p = ")";
  1227. break;
  1228. case ATOM_INTEGER:
  1229. i = *((const int *) v);
  1230. if (i < 0)
  1231. gfc_internal_error ("write_atom(): Writing negative integer");
  1232. sprintf (buffer, "%d", i);
  1233. p = buffer;
  1234. break;
  1235. default:
  1236. gfc_internal_error ("write_atom(): Trying to write dab atom");
  1237. }
  1238. if(p == NULL || *p == '\0')
  1239. len = 0;
  1240. else
  1241. len = strlen (p);
  1242. if (atom != ATOM_RPAREN)
  1243. {
  1244. if (module_column + len > 72)
  1245. write_char ('\n');
  1246. else
  1247. {
  1248. if (last_atom != ATOM_LPAREN && module_column != 1)
  1249. write_char (' ');
  1250. }
  1251. }
  1252. if (atom == ATOM_STRING)
  1253. write_char ('\'');
  1254. while (p != NULL && *p)
  1255. {
  1256. if (atom == ATOM_STRING && *p == '\'')
  1257. write_char ('\'');
  1258. write_char (*p++);
  1259. }
  1260. if (atom == ATOM_STRING)
  1261. write_char ('\'');
  1262. last_atom = atom;
  1263. }
  1264. /***************** Mid-level I/O subroutines *****************/
  1265. /* These subroutines let their caller read or write atoms without
  1266. caring about which of the two is actually happening. This lets a
  1267. subroutine concentrate on the actual format of the data being
  1268. written. */
  1269. static void mio_expr (gfc_expr **);
  1270. pointer_info *mio_symbol_ref (gfc_symbol **);
  1271. pointer_info *mio_interface_rest (gfc_interface **);
  1272. static void mio_symtree_ref (gfc_symtree **);
  1273. /* Read or write an enumerated value. On writing, we return the input
  1274. value for the convenience of callers. We avoid using an integer
  1275. pointer because enums are sometimes inside bitfields. */
  1276. static int
  1277. mio_name (int t, const mstring *m)
  1278. {
  1279. if (iomode == IO_OUTPUT)
  1280. write_atom (ATOM_NAME, gfc_code2string (m, t));
  1281. else
  1282. {
  1283. require_atom (ATOM_NAME);
  1284. t = find_enum (m);
  1285. }
  1286. return t;
  1287. }
  1288. /* Specialization of mio_name. */
  1289. #define DECL_MIO_NAME(TYPE) \
  1290. static inline TYPE \
  1291. MIO_NAME(TYPE) (TYPE t, const mstring *m) \
  1292. { \
  1293. return (TYPE) mio_name ((int) t, m); \
  1294. }
  1295. #define MIO_NAME(TYPE) mio_name_##TYPE
  1296. static void
  1297. mio_lparen (void)
  1298. {
  1299. if (iomode == IO_OUTPUT)
  1300. write_atom (ATOM_LPAREN, NULL);
  1301. else
  1302. require_atom (ATOM_LPAREN);
  1303. }
  1304. static void
  1305. mio_rparen (void)
  1306. {
  1307. if (iomode == IO_OUTPUT)
  1308. write_atom (ATOM_RPAREN, NULL);
  1309. else
  1310. require_atom (ATOM_RPAREN);
  1311. }
  1312. static void
  1313. mio_integer (int *ip)
  1314. {
  1315. if (iomode == IO_OUTPUT)
  1316. write_atom (ATOM_INTEGER, ip);
  1317. else
  1318. {
  1319. require_atom (ATOM_INTEGER);
  1320. *ip = atom_int;
  1321. }
  1322. }
  1323. /* Read or write a gfc_intrinsic_op value. */
  1324. static void
  1325. mio_intrinsic_op (gfc_intrinsic_op* op)
  1326. {
  1327. /* FIXME: Would be nicer to do this via the operators symbolic name. */
  1328. if (iomode == IO_OUTPUT)
  1329. {
  1330. int converted = (int) *op;
  1331. write_atom (ATOM_INTEGER, &converted);
  1332. }
  1333. else
  1334. {
  1335. require_atom (ATOM_INTEGER);
  1336. *op = (gfc_intrinsic_op) atom_int;
  1337. }
  1338. }
  1339. /* Read or write a character pointer that points to a string on the heap. */
  1340. static const char *
  1341. mio_allocated_string (const char *s)
  1342. {
  1343. if (iomode == IO_OUTPUT)
  1344. {
  1345. write_atom (ATOM_STRING, s);
  1346. return s;
  1347. }
  1348. else
  1349. {
  1350. require_atom (ATOM_STRING);
  1351. return atom_string;
  1352. }
  1353. }
  1354. /* Functions for quoting and unquoting strings. */
  1355. static char *
  1356. quote_string (const gfc_char_t *s, const size_t slength)
  1357. {
  1358. const gfc_char_t *p;
  1359. char *res, *q;
  1360. size_t len = 0, i;
  1361. /* Calculate the length we'll need: a backslash takes two ("\\"),
  1362. non-printable characters take 10 ("\Uxxxxxxxx") and others take 1. */
  1363. for (p = s, i = 0; i < slength; p++, i++)
  1364. {
  1365. if (*p == '\\')
  1366. len += 2;
  1367. else if (!gfc_wide_is_printable (*p))
  1368. len += 10;
  1369. else
  1370. len++;
  1371. }
  1372. q = res = XCNEWVEC (char, len + 1);
  1373. for (p = s, i = 0; i < slength; p++, i++)
  1374. {
  1375. if (*p == '\\')
  1376. *q++ = '\\', *q++ = '\\';
  1377. else if (!gfc_wide_is_printable (*p))
  1378. {
  1379. sprintf (q, "\\U%08" HOST_WIDE_INT_PRINT "x",
  1380. (unsigned HOST_WIDE_INT) *p);
  1381. q += 10;
  1382. }
  1383. else
  1384. *q++ = (unsigned char) *p;
  1385. }
  1386. res[len] = '\0';
  1387. return res;
  1388. }
  1389. static gfc_char_t *
  1390. unquote_string (const char *s)
  1391. {
  1392. size_t len, i;
  1393. const char *p;
  1394. gfc_char_t *res;
  1395. for (p = s, len = 0; *p; p++, len++)
  1396. {
  1397. if (*p != '\\')
  1398. continue;
  1399. if (p[1] == '\\')
  1400. p++;
  1401. else if (p[1] == 'U')
  1402. p += 9; /* That is a "\U????????". */
  1403. else
  1404. gfc_internal_error ("unquote_string(): got bad string");
  1405. }
  1406. res = gfc_get_wide_string (len + 1);
  1407. for (i = 0, p = s; i < len; i++, p++)
  1408. {
  1409. gcc_assert (*p);
  1410. if (*p != '\\')
  1411. res[i] = (unsigned char) *p;
  1412. else if (p[1] == '\\')
  1413. {
  1414. res[i] = (unsigned char) '\\';
  1415. p++;
  1416. }
  1417. else
  1418. {
  1419. /* We read the 8-digits hexadecimal constant that follows. */
  1420. int j;
  1421. unsigned n;
  1422. gfc_char_t c = 0;
  1423. gcc_assert (p[1] == 'U');
  1424. for (j = 0; j < 8; j++)
  1425. {
  1426. c = c << 4;
  1427. gcc_assert (sscanf (&p[j+2], "%01x", &n) == 1);
  1428. c += n;
  1429. }
  1430. res[i] = c;
  1431. p += 9;
  1432. }
  1433. }
  1434. res[len] = '\0';
  1435. return res;
  1436. }
  1437. /* Read or write a character pointer that points to a wide string on the
  1438. heap, performing quoting/unquoting of nonprintable characters using the
  1439. form \U???????? (where each ? is a hexadecimal digit).
  1440. Length is the length of the string, only known and used in output mode. */
  1441. static const gfc_char_t *
  1442. mio_allocated_wide_string (const gfc_char_t *s, const size_t length)
  1443. {
  1444. if (iomode == IO_OUTPUT)
  1445. {
  1446. char *quoted = quote_string (s, length);
  1447. write_atom (ATOM_STRING, quoted);
  1448. free (quoted);
  1449. return s;
  1450. }
  1451. else
  1452. {
  1453. gfc_char_t *unquoted;
  1454. require_atom (ATOM_STRING);
  1455. unquoted = unquote_string (atom_string);
  1456. free (atom_string);
  1457. return unquoted;
  1458. }
  1459. }
  1460. /* Read or write a string that is in static memory. */
  1461. static void
  1462. mio_pool_string (const char **stringp)
  1463. {
  1464. /* TODO: one could write the string only once, and refer to it via a
  1465. fixup pointer. */
  1466. /* As a special case we have to deal with a NULL string. This
  1467. happens for the 'module' member of 'gfc_symbol's that are not in a
  1468. module. We read / write these as the empty string. */
  1469. if (iomode == IO_OUTPUT)
  1470. {
  1471. const char *p = *stringp == NULL ? "" : *stringp;
  1472. write_atom (ATOM_STRING, p);
  1473. }
  1474. else
  1475. {
  1476. require_atom (ATOM_STRING);
  1477. *stringp = atom_string[0] == '\0' ? NULL : gfc_get_string (atom_string);
  1478. free (atom_string);
  1479. }
  1480. }
  1481. /* Read or write a string that is inside of some already-allocated
  1482. structure. */
  1483. static void
  1484. mio_internal_string (char *string)
  1485. {
  1486. if (iomode == IO_OUTPUT)
  1487. write_atom (ATOM_STRING, string);
  1488. else
  1489. {
  1490. require_atom (ATOM_STRING);
  1491. strcpy (string, atom_string);
  1492. free (atom_string);
  1493. }
  1494. }
  1495. typedef enum
  1496. { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
  1497. AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
  1498. AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
  1499. AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
  1500. AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE,
  1501. AB_ALLOC_COMP, AB_POINTER_COMP, AB_PROC_POINTER_COMP, AB_PRIVATE_COMP,
  1502. AB_VALUE, AB_VOLATILE, AB_PROTECTED, AB_LOCK_COMP,
  1503. AB_IS_BIND_C, AB_IS_C_INTEROP, AB_IS_ISO_C, AB_ABSTRACT, AB_ZERO_COMP,
  1504. AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
  1505. AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
  1506. AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
  1507. AB_ARRAY_OUTER_DEPENDENCY
  1508. }
  1509. ab_attribute;
  1510. static const mstring attr_bits[] =
  1511. {
  1512. minit ("ALLOCATABLE", AB_ALLOCATABLE),
  1513. minit ("ARTIFICIAL", AB_ARTIFICIAL),
  1514. minit ("ASYNCHRONOUS", AB_ASYNCHRONOUS),
  1515. minit ("DIMENSION", AB_DIMENSION),
  1516. minit ("CODIMENSION", AB_CODIMENSION),
  1517. minit ("CONTIGUOUS", AB_CONTIGUOUS),
  1518. minit ("EXTERNAL", AB_EXTERNAL),
  1519. minit ("INTRINSIC", AB_INTRINSIC),
  1520. minit ("OPTIONAL", AB_OPTIONAL),
  1521. minit ("POINTER", AB_POINTER),
  1522. minit ("VOLATILE", AB_VOLATILE),
  1523. minit ("TARGET", AB_TARGET),
  1524. minit ("THREADPRIVATE", AB_THREADPRIVATE),
  1525. minit ("DUMMY", AB_DUMMY),
  1526. minit ("RESULT", AB_RESULT),
  1527. minit ("DATA", AB_DATA),
  1528. minit ("IN_NAMELIST", AB_IN_NAMELIST),
  1529. minit ("IN_COMMON", AB_IN_COMMON),
  1530. minit ("FUNCTION", AB_FUNCTION),
  1531. minit ("SUBROUTINE", AB_SUBROUTINE),
  1532. minit ("SEQUENCE", AB_SEQUENCE),
  1533. minit ("ELEMENTAL", AB_ELEMENTAL),
  1534. minit ("PURE", AB_PURE),
  1535. minit ("RECURSIVE", AB_RECURSIVE),
  1536. minit ("GENERIC", AB_GENERIC),
  1537. minit ("ALWAYS_EXPLICIT", AB_ALWAYS_EXPLICIT),
  1538. minit ("CRAY_POINTER", AB_CRAY_POINTER),
  1539. minit ("CRAY_POINTEE", AB_CRAY_POINTEE),
  1540. minit ("IS_BIND_C", AB_IS_BIND_C),
  1541. minit ("IS_C_INTEROP", AB_IS_C_INTEROP),
  1542. minit ("IS_ISO_C", AB_IS_ISO_C),
  1543. minit ("VALUE", AB_VALUE),
  1544. minit ("ALLOC_COMP", AB_ALLOC_COMP),
  1545. minit ("COARRAY_COMP", AB_COARRAY_COMP),
  1546. minit ("LOCK_COMP", AB_LOCK_COMP),
  1547. minit ("POINTER_COMP", AB_POINTER_COMP),
  1548. minit ("PROC_POINTER_COMP", AB_PROC_POINTER_COMP),
  1549. minit ("PRIVATE_COMP", AB_PRIVATE_COMP),
  1550. minit ("ZERO_COMP", AB_ZERO_COMP),
  1551. minit ("PROTECTED", AB_PROTECTED),
  1552. minit ("ABSTRACT", AB_ABSTRACT),
  1553. minit ("IS_CLASS", AB_IS_CLASS),
  1554. minit ("PROCEDURE", AB_PROCEDURE),
  1555. minit ("PROC_POINTER", AB_PROC_POINTER),
  1556. minit ("VTYPE", AB_VTYPE),
  1557. minit ("VTAB", AB_VTAB),
  1558. minit ("CLASS_POINTER", AB_CLASS_POINTER),
  1559. minit ("IMPLICIT_PURE", AB_IMPLICIT_PURE),
  1560. minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
  1561. minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
  1562. minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
  1563. minit (NULL, -1)
  1564. };
  1565. /* For binding attributes. */
  1566. static const mstring binding_passing[] =
  1567. {
  1568. minit ("PASS", 0),
  1569. minit ("NOPASS", 1),
  1570. minit (NULL, -1)
  1571. };
  1572. static const mstring binding_overriding[] =
  1573. {
  1574. minit ("OVERRIDABLE", 0),
  1575. minit ("NON_OVERRIDABLE", 1),
  1576. minit ("DEFERRED", 2),
  1577. minit (NULL, -1)
  1578. };
  1579. static const mstring binding_generic[] =
  1580. {
  1581. minit ("SPECIFIC", 0),
  1582. minit ("GENERIC", 1),
  1583. minit (NULL, -1)
  1584. };
  1585. static const mstring binding_ppc[] =
  1586. {
  1587. minit ("NO_PPC", 0),
  1588. minit ("PPC", 1),
  1589. minit (NULL, -1)
  1590. };
  1591. /* Specialization of mio_name. */
  1592. DECL_MIO_NAME (ab_attribute)
  1593. DECL_MIO_NAME (ar_type)
  1594. DECL_MIO_NAME (array_type)
  1595. DECL_MIO_NAME (bt)
  1596. DECL_MIO_NAME (expr_t)
  1597. DECL_MIO_NAME (gfc_access)
  1598. DECL_MIO_NAME (gfc_intrinsic_op)
  1599. DECL_MIO_NAME (ifsrc)
  1600. DECL_MIO_NAME (save_state)
  1601. DECL_MIO_NAME (procedure_type)
  1602. DECL_MIO_NAME (ref_type)
  1603. DECL_MIO_NAME (sym_flavor)
  1604. DECL_MIO_NAME (sym_intent)
  1605. #undef DECL_MIO_NAME
  1606. /* Symbol attributes are stored in list with the first three elements
  1607. being the enumerated fields, while the remaining elements (if any)
  1608. indicate the individual attribute bits. The access field is not
  1609. saved-- it controls what symbols are exported when a module is
  1610. written. */
  1611. static void
  1612. mio_symbol_attribute (symbol_attribute *attr)
  1613. {
  1614. atom_type t;
  1615. unsigned ext_attr,extension_level;
  1616. mio_lparen ();
  1617. attr->flavor = MIO_NAME (sym_flavor) (attr->flavor, flavors);
  1618. attr->intent = MIO_NAME (sym_intent) (attr->intent, intents);
  1619. attr->proc = MIO_NAME (procedure_type) (attr->proc, procedures);
  1620. attr->if_source = MIO_NAME (ifsrc) (attr->if_source, ifsrc_types);
  1621. attr->save = MIO_NAME (save_state) (attr->save, save_status);
  1622. ext_attr = attr->ext_attr;
  1623. mio_integer ((int *) &ext_attr);
  1624. attr->ext_attr = ext_attr;
  1625. extension_level = attr->extension;
  1626. mio_integer ((int *) &extension_level);
  1627. attr->extension = extension_level;
  1628. if (iomode == IO_OUTPUT)
  1629. {
  1630. if (attr->allocatable)
  1631. MIO_NAME (ab_attribute) (AB_ALLOCATABLE, attr_bits);
  1632. if (attr->artificial)
  1633. MIO_NAME (ab_attribute) (AB_ARTIFICIAL, attr_bits);
  1634. if (attr->asynchronous)
  1635. MIO_NAME (ab_attribute) (AB_ASYNCHRONOUS, attr_bits);
  1636. if (attr->dimension)
  1637. MIO_NAME (ab_attribute) (AB_DIMENSION, attr_bits);
  1638. if (attr->codimension)
  1639. MIO_NAME (ab_attribute) (AB_CODIMENSION, attr_bits);
  1640. if (attr->contiguous)
  1641. MIO_NAME (ab_attribute) (AB_CONTIGUOUS, attr_bits);
  1642. if (attr->external)
  1643. MIO_NAME (ab_attribute) (AB_EXTERNAL, attr_bits);
  1644. if (attr->intrinsic)
  1645. MIO_NAME (ab_attribute) (AB_INTRINSIC, attr_bits);
  1646. if (attr->optional)
  1647. MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
  1648. if (attr->pointer)
  1649. MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
  1650. if (attr->class_pointer)
  1651. MIO_NAME (ab_attribute) (AB_CLASS_POINTER, attr_bits);
  1652. if (attr->is_protected)
  1653. MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
  1654. if (attr->value)
  1655. MIO_NAME (ab_attribute) (AB_VALUE, attr_bits);
  1656. if (attr->volatile_)
  1657. MIO_NAME (ab_attribute) (AB_VOLATILE, attr_bits);
  1658. if (attr->target)
  1659. MIO_NAME (ab_attribute) (AB_TARGET, attr_bits);
  1660. if (attr->threadprivate)
  1661. MIO_NAME (ab_attribute) (AB_THREADPRIVATE, attr_bits);
  1662. if (attr->dummy)
  1663. MIO_NAME (ab_attribute) (AB_DUMMY, attr_bits);
  1664. if (attr->result)
  1665. MIO_NAME (ab_attribute) (AB_RESULT, attr_bits);
  1666. /* We deliberately don't preserve the "entry" flag. */
  1667. if (attr->data)
  1668. MIO_NAME (ab_attribute) (AB_DATA, attr_bits);
  1669. if (attr->in_namelist)
  1670. MIO_NAME (ab_attribute) (AB_IN_NAMELIST, attr_bits);
  1671. if (attr->in_common)
  1672. MIO_NAME (ab_attribute) (AB_IN_COMMON, attr_bits);
  1673. if (attr->function)
  1674. MIO_NAME (ab_attribute) (AB_FUNCTION, attr_bits);
  1675. if (attr->subroutine)
  1676. MIO_NAME (ab_attribute) (AB_SUBROUTINE, attr_bits);
  1677. if (attr->generic)
  1678. MIO_NAME (ab_attribute) (AB_GENERIC, attr_bits);
  1679. if (attr->abstract)
  1680. MIO_NAME (ab_attribute) (AB_ABSTRACT, attr_bits);
  1681. if (attr->sequence)
  1682. MIO_NAME (ab_attribute) (AB_SEQUENCE, attr_bits);
  1683. if (attr->elemental)
  1684. MIO_NAME (ab_attribute) (AB_ELEMENTAL, attr_bits);
  1685. if (attr->pure)
  1686. MIO_NAME (ab_attribute) (AB_PURE, attr_bits);
  1687. if (attr->implicit_pure)
  1688. MIO_NAME (ab_attribute) (AB_IMPLICIT_PURE, attr_bits);
  1689. if (attr->unlimited_polymorphic)
  1690. MIO_NAME (ab_attribute) (AB_UNLIMITED_POLY, attr_bits);
  1691. if (attr->recursive)
  1692. MIO_NAME (ab_attribute) (AB_RECURSIVE, attr_bits);
  1693. if (attr->always_explicit)
  1694. MIO_NAME (ab_attribute) (AB_ALWAYS_EXPLICIT, attr_bits);
  1695. if (attr->cray_pointer)
  1696. MIO_NAME (ab_attribute) (AB_CRAY_POINTER, attr_bits);
  1697. if (attr->cray_pointee)
  1698. MIO_NAME (ab_attribute) (AB_CRAY_POINTEE, attr_bits);
  1699. if (attr->is_bind_c)
  1700. MIO_NAME(ab_attribute) (AB_IS_BIND_C, attr_bits);
  1701. if (attr->is_c_interop)
  1702. MIO_NAME(ab_attribute) (AB_IS_C_INTEROP, attr_bits);
  1703. if (attr->is_iso_c)
  1704. MIO_NAME(ab_attribute) (AB_IS_ISO_C, attr_bits);
  1705. if (attr->alloc_comp)
  1706. MIO_NAME (ab_attribute) (AB_ALLOC_COMP, attr_bits);
  1707. if (attr->pointer_comp)
  1708. MIO_NAME (ab_attribute) (AB_POINTER_COMP, attr_bits);
  1709. if (attr->proc_pointer_comp)
  1710. MIO_NAME (ab_attribute) (AB_PROC_POINTER_COMP, attr_bits);
  1711. if (attr->private_comp)
  1712. MIO_NAME (ab_attribute) (AB_PRIVATE_COMP, attr_bits);
  1713. if (attr->coarray_comp)
  1714. MIO_NAME (ab_attribute) (AB_COARRAY_COMP, attr_bits);
  1715. if (attr->lock_comp)
  1716. MIO_NAME (ab_attribute) (AB_LOCK_COMP, attr_bits);
  1717. if (attr->zero_comp)
  1718. MIO_NAME (ab_attribute) (AB_ZERO_COMP, attr_bits);
  1719. if (attr->is_class)
  1720. MIO_NAME (ab_attribute) (AB_IS_CLASS, attr_bits);
  1721. if (attr->procedure)
  1722. MIO_NAME (ab_attribute) (AB_PROCEDURE, attr_bits);
  1723. if (attr->proc_pointer)
  1724. MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
  1725. if (attr->vtype)
  1726. MIO_NAME (ab_attribute) (AB_VTYPE, attr_bits);
  1727. if (attr->vtab)
  1728. MIO_NAME (ab_attribute) (AB_VTAB, attr_bits);
  1729. if (attr->omp_declare_target)
  1730. MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
  1731. if (attr->array_outer_dependency)
  1732. MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
  1733. mio_rparen ();
  1734. }
  1735. else
  1736. {
  1737. for (;;)
  1738. {
  1739. t = parse_atom ();
  1740. if (t == ATOM_RPAREN)
  1741. break;
  1742. if (t != ATOM_NAME)
  1743. bad_module ("Expected attribute bit name");
  1744. switch ((ab_attribute) find_enum (attr_bits))
  1745. {
  1746. case AB_ALLOCATABLE:
  1747. attr->allocatable = 1;
  1748. break;
  1749. case AB_ARTIFICIAL:
  1750. attr->artificial = 1;
  1751. break;
  1752. case AB_ASYNCHRONOUS:
  1753. attr->asynchronous = 1;
  1754. break;
  1755. case AB_DIMENSION:
  1756. attr->dimension = 1;
  1757. break;
  1758. case AB_CODIMENSION:
  1759. attr->codimension = 1;
  1760. break;
  1761. case AB_CONTIGUOUS:
  1762. attr->contiguous = 1;
  1763. break;
  1764. case AB_EXTERNAL:
  1765. attr->external = 1;
  1766. break;
  1767. case AB_INTRINSIC:
  1768. attr->intrinsic = 1;
  1769. break;
  1770. case AB_OPTIONAL:
  1771. attr->optional = 1;
  1772. break;
  1773. case AB_POINTER:
  1774. attr->pointer = 1;
  1775. break;
  1776. case AB_CLASS_POINTER:
  1777. attr->class_pointer = 1;
  1778. break;
  1779. case AB_PROTECTED:
  1780. attr->is_protected = 1;
  1781. break;
  1782. case AB_VALUE:
  1783. attr->value = 1;
  1784. break;
  1785. case AB_VOLATILE:
  1786. attr->volatile_ = 1;
  1787. break;
  1788. case AB_TARGET:
  1789. attr->target = 1;
  1790. break;
  1791. case AB_THREADPRIVATE:
  1792. attr->threadprivate = 1;
  1793. break;
  1794. case AB_DUMMY:
  1795. attr->dummy = 1;
  1796. break;
  1797. case AB_RESULT:
  1798. attr->result = 1;
  1799. break;
  1800. case AB_DATA:
  1801. attr->data = 1;
  1802. break;
  1803. case AB_IN_NAMELIST:
  1804. attr->in_namelist = 1;
  1805. break;
  1806. case AB_IN_COMMON:
  1807. attr->in_common = 1;
  1808. break;
  1809. case AB_FUNCTION:
  1810. attr->function = 1;
  1811. break;
  1812. case AB_SUBROUTINE:
  1813. attr->subroutine = 1;
  1814. break;
  1815. case AB_GENERIC:
  1816. attr->generic = 1;
  1817. break;
  1818. case AB_ABSTRACT:
  1819. attr->abstract = 1;
  1820. break;
  1821. case AB_SEQUENCE:
  1822. attr->sequence = 1;
  1823. break;
  1824. case AB_ELEMENTAL:
  1825. attr->elemental = 1;
  1826. break;
  1827. case AB_PURE:
  1828. attr->pure = 1;
  1829. break;
  1830. case AB_IMPLICIT_PURE:
  1831. attr->implicit_pure = 1;
  1832. break;
  1833. case AB_UNLIMITED_POLY:
  1834. attr->unlimited_polymorphic = 1;
  1835. break;
  1836. case AB_RECURSIVE:
  1837. attr->recursive = 1;
  1838. break;
  1839. case AB_ALWAYS_EXPLICIT:
  1840. attr->always_explicit = 1;
  1841. break;
  1842. case AB_CRAY_POINTER:
  1843. attr->cray_pointer = 1;
  1844. break;
  1845. case AB_CRAY_POINTEE:
  1846. attr->cray_pointee = 1;
  1847. break;
  1848. case AB_IS_BIND_C:
  1849. attr->is_bind_c = 1;
  1850. break;
  1851. case AB_IS_C_INTEROP:
  1852. attr->is_c_interop = 1;
  1853. break;
  1854. case AB_IS_ISO_C:
  1855. attr->is_iso_c = 1;
  1856. break;
  1857. case AB_ALLOC_COMP:
  1858. attr->alloc_comp = 1;
  1859. break;
  1860. case AB_COARRAY_COMP:
  1861. attr->coarray_comp = 1;
  1862. break;
  1863. case AB_LOCK_COMP:
  1864. attr->lock_comp = 1;
  1865. break;
  1866. case AB_POINTER_COMP:
  1867. attr->pointer_comp = 1;
  1868. break;
  1869. case AB_PROC_POINTER_COMP:
  1870. attr->proc_pointer_comp = 1;
  1871. break;
  1872. case AB_PRIVATE_COMP:
  1873. attr->private_comp = 1;
  1874. break;
  1875. case AB_ZERO_COMP:
  1876. attr->zero_comp = 1;
  1877. break;
  1878. case AB_IS_CLASS:
  1879. attr->is_class = 1;
  1880. break;
  1881. case AB_PROCEDURE:
  1882. attr->procedure = 1;
  1883. break;
  1884. case AB_PROC_POINTER:
  1885. attr->proc_pointer = 1;
  1886. break;
  1887. case AB_VTYPE:
  1888. attr->vtype = 1;
  1889. break;
  1890. case AB_VTAB:
  1891. attr->vtab = 1;
  1892. break;
  1893. case AB_OMP_DECLARE_TARGET:
  1894. attr->omp_declare_target = 1;
  1895. break;
  1896. case AB_ARRAY_OUTER_DEPENDENCY:
  1897. attr->array_outer_dependency =1;
  1898. break;
  1899. }
  1900. }
  1901. }
  1902. }
  1903. static const mstring bt_types[] = {
  1904. minit ("INTEGER", BT_INTEGER),
  1905. minit ("REAL", BT_REAL),
  1906. minit ("COMPLEX", BT_COMPLEX),
  1907. minit ("LOGICAL", BT_LOGICAL),
  1908. minit ("CHARACTER", BT_CHARACTER),
  1909. minit ("DERIVED", BT_DERIVED),
  1910. minit ("CLASS", BT_CLASS),
  1911. minit ("PROCEDURE", BT_PROCEDURE),
  1912. minit ("UNKNOWN", BT_UNKNOWN),
  1913. minit ("VOID", BT_VOID),
  1914. minit ("ASSUMED", BT_ASSUMED),
  1915. minit (NULL, -1)
  1916. };
  1917. static void
  1918. mio_charlen (gfc_charlen **clp)
  1919. {
  1920. gfc_charlen *cl;
  1921. mio_lparen ();
  1922. if (iomode == IO_OUTPUT)
  1923. {
  1924. cl = *clp;
  1925. if (cl != NULL)
  1926. mio_expr (&cl->length);
  1927. }
  1928. else
  1929. {
  1930. if (peek_atom () != ATOM_RPAREN)
  1931. {
  1932. cl = gfc_new_charlen (gfc_current_ns, NULL);
  1933. mio_expr (&cl->length);
  1934. *clp = cl;
  1935. }
  1936. }
  1937. mio_rparen ();
  1938. }
  1939. /* See if a name is a generated name. */
  1940. static int
  1941. check_unique_name (const char *name)
  1942. {
  1943. return *name == '@';
  1944. }
  1945. static void
  1946. mio_typespec (gfc_typespec *ts)
  1947. {
  1948. mio_lparen ();
  1949. ts->type = MIO_NAME (bt) (ts->type, bt_types);
  1950. if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
  1951. mio_integer (&ts->kind);
  1952. else
  1953. mio_symbol_ref (&ts->u.derived);
  1954. mio_symbol_ref (&ts->interface);
  1955. /* Add info for C interop and is_iso_c. */
  1956. mio_integer (&ts->is_c_interop);
  1957. mio_integer (&ts->is_iso_c);
  1958. /* If the typespec is for an identifier either from iso_c_binding, or
  1959. a constant that was initialized to an identifier from it, use the
  1960. f90_type. Otherwise, use the ts->type, since it shouldn't matter. */
  1961. if (ts->is_iso_c)
  1962. ts->f90_type = MIO_NAME (bt) (ts->f90_type, bt_types);
  1963. else
  1964. ts->f90_type = MIO_NAME (bt) (ts->type, bt_types);
  1965. if (ts->type != BT_CHARACTER)
  1966. {
  1967. /* ts->u.cl is only valid for BT_CHARACTER. */
  1968. mio_lparen ();
  1969. mio_rparen ();
  1970. }
  1971. else
  1972. mio_charlen (&ts->u.cl);
  1973. /* So as not to disturb the existing API, use an ATOM_NAME to
  1974. transmit deferred characteristic for characters (F2003). */
  1975. if (iomode == IO_OUTPUT)
  1976. {
  1977. if (ts->type == BT_CHARACTER && ts->deferred)
  1978. write_atom (ATOM_NAME, "DEFERRED_CL");
  1979. }
  1980. else if (peek_atom () != ATOM_RPAREN)
  1981. {
  1982. if (parse_atom () != ATOM_NAME)
  1983. bad_module ("Expected string");
  1984. ts->deferred = 1;
  1985. }
  1986. mio_rparen ();
  1987. }
  1988. static const mstring array_spec_types[] = {
  1989. minit ("EXPLICIT", AS_EXPLICIT),
  1990. minit ("ASSUMED_RANK", AS_ASSUMED_RANK),
  1991. minit ("ASSUMED_SHAPE", AS_ASSUMED_SHAPE),
  1992. minit ("DEFERRED", AS_DEFERRED),
  1993. minit ("ASSUMED_SIZE", AS_ASSUMED_SIZE),
  1994. minit (NULL, -1)
  1995. };
  1996. static void
  1997. mio_array_spec (gfc_array_spec **asp)
  1998. {
  1999. gfc_array_spec *as;
  2000. int i;
  2001. mio_lparen ();
  2002. if (iomode == IO_OUTPUT)
  2003. {
  2004. int rank;
  2005. if (*asp == NULL)
  2006. goto done;
  2007. as = *asp;
  2008. /* mio_integer expects nonnegative values. */
  2009. rank = as->rank > 0 ? as->rank : 0;
  2010. mio_integer (&rank);
  2011. }
  2012. else
  2013. {
  2014. if (peek_atom () == ATOM_RPAREN)
  2015. {
  2016. *asp = NULL;
  2017. goto done;
  2018. }
  2019. *asp = as = gfc_get_array_spec ();
  2020. mio_integer (&as->rank);
  2021. }
  2022. mio_integer (&as->corank);
  2023. as->type = MIO_NAME (array_type) (as->type, array_spec_types);
  2024. if (iomode == IO_INPUT && as->type == AS_ASSUMED_RANK)
  2025. as->rank = -1;
  2026. if (iomode == IO_INPUT && as->corank)
  2027. as->cotype = (as->type == AS_DEFERRED) ? AS_DEFERRED : AS_EXPLICIT;
  2028. if (as->rank + as->corank > 0)
  2029. for (i = 0; i < as->rank + as->corank; i++)
  2030. {
  2031. mio_expr (&as->lower[i]);
  2032. mio_expr (&as->upper[i]);
  2033. }
  2034. done:
  2035. mio_rparen ();
  2036. }
  2037. /* Given a pointer to an array reference structure (which lives in a
  2038. gfc_ref structure), find the corresponding array specification
  2039. structure. Storing the pointer in the ref structure doesn't quite
  2040. work when loading from a module. Generating code for an array
  2041. reference also needs more information than just the array spec. */
  2042. static const mstring array_ref_types[] = {
  2043. minit ("FULL", AR_FULL),
  2044. minit ("ELEMENT", AR_ELEMENT),
  2045. minit ("SECTION", AR_SECTION),
  2046. minit (NULL, -1)
  2047. };
  2048. static void
  2049. mio_array_ref (gfc_array_ref *ar)
  2050. {
  2051. int i;
  2052. mio_lparen ();
  2053. ar->type = MIO_NAME (ar_type) (ar->type, array_ref_types);
  2054. mio_integer (&ar->dimen);
  2055. switch (ar->type)
  2056. {
  2057. case AR_FULL:
  2058. break;
  2059. case AR_ELEMENT:
  2060. for (i = 0; i < ar->dimen; i++)
  2061. mio_expr (&ar->start[i]);
  2062. break;
  2063. case AR_SECTION:
  2064. for (i = 0; i < ar->dimen; i++)
  2065. {
  2066. mio_expr (&ar->start[i]);
  2067. mio_expr (&ar->end[i]);
  2068. mio_expr (&ar->stride[i]);
  2069. }
  2070. break;
  2071. case AR_UNKNOWN:
  2072. gfc_internal_error ("mio_array_ref(): Unknown array ref");
  2073. }
  2074. /* Unfortunately, ar->dimen_type is an anonymous enumerated type so
  2075. we can't call mio_integer directly. Instead loop over each element
  2076. and cast it to/from an integer. */
  2077. if (iomode == IO_OUTPUT)
  2078. {
  2079. for (i = 0; i < ar->dimen; i++)
  2080. {
  2081. int tmp = (int)ar->dimen_type[i];
  2082. write_atom (ATOM_INTEGER, &tmp);
  2083. }
  2084. }
  2085. else
  2086. {
  2087. for (i = 0; i < ar->dimen; i++)
  2088. {
  2089. require_atom (ATOM_INTEGER);
  2090. ar->dimen_type[i] = (enum gfc_array_ref_dimen_type) atom_int;
  2091. }
  2092. }
  2093. if (iomode == IO_INPUT)
  2094. {
  2095. ar->where = gfc_current_locus;
  2096. for (i = 0; i < ar->dimen; i++)
  2097. ar->c_where[i] = gfc_current_locus;
  2098. }
  2099. mio_rparen ();
  2100. }
  2101. /* Saves or restores a pointer. The pointer is converted back and
  2102. forth from an integer. We return the pointer_info pointer so that
  2103. the caller can take additional action based on the pointer type. */
  2104. static pointer_info *
  2105. mio_pointer_ref (void *gp)
  2106. {
  2107. pointer_info *p;
  2108. if (iomode == IO_OUTPUT)
  2109. {
  2110. p = get_pointer (*((char **) gp));
  2111. write_atom (ATOM_INTEGER, &p->integer);
  2112. }
  2113. else
  2114. {
  2115. require_atom (ATOM_INTEGER);
  2116. p = add_fixup (atom_int, gp);
  2117. }
  2118. return p;
  2119. }
  2120. /* Save and load references to components that occur within
  2121. expressions. We have to describe these references by a number and
  2122. by name. The number is necessary for forward references during
  2123. reading, and the name is necessary if the symbol already exists in
  2124. the namespace and is not loaded again. */
  2125. static void
  2126. mio_component_ref (gfc_component **cp)
  2127. {
  2128. pointer_info *p;
  2129. p = mio_pointer_ref (cp);
  2130. if (p->type == P_UNKNOWN)
  2131. p->type = P_COMPONENT;
  2132. }
  2133. static void mio_namespace_ref (gfc_namespace **nsp);
  2134. static void mio_formal_arglist (gfc_formal_arglist **formal);
  2135. static void mio_typebound_proc (gfc_typebound_proc** proc);
  2136. static void
  2137. mio_component (gfc_component *c, int vtype)
  2138. {
  2139. pointer_info *p;
  2140. int n;
  2141. mio_lparen ();
  2142. if (iomode == IO_OUTPUT)
  2143. {
  2144. p = get_pointer (c);
  2145. mio_integer (&p->integer);
  2146. }
  2147. else
  2148. {
  2149. mio_integer (&n);
  2150. p = get_integer (n);
  2151. associate_integer_pointer (p, c);
  2152. }
  2153. if (p->type == P_UNKNOWN)
  2154. p->type = P_COMPONENT;
  2155. mio_pool_string (&c->name);
  2156. mio_typespec (&c->ts);
  2157. mio_array_spec (&c->as);
  2158. mio_symbol_attribute (&c->attr);
  2159. if (c->ts.type == BT_CLASS)
  2160. c->attr.class_ok = 1;
  2161. c->attr.access = MIO_NAME (gfc_access) (c->attr.access, access_types);
  2162. if (!vtype || strcmp (c->name, "_final") == 0
  2163. || strcmp (c->name, "_hash") == 0)
  2164. mio_expr (&c->initializer);
  2165. if (c->attr.proc_pointer)
  2166. mio_typebound_proc (&c->tb);
  2167. mio_rparen ();
  2168. }
  2169. static void
  2170. mio_component_list (gfc_component **cp, int vtype)
  2171. {
  2172. gfc_component *c, *tail;
  2173. mio_lparen ();
  2174. if (iomode == IO_OUTPUT)
  2175. {
  2176. for (c = *cp; c; c = c->next)
  2177. mio_component (c, vtype);
  2178. }
  2179. else
  2180. {
  2181. *cp = NULL;
  2182. tail = NULL;
  2183. for (;;)
  2184. {
  2185. if (peek_atom () == ATOM_RPAREN)
  2186. break;
  2187. c = gfc_get_component ();
  2188. mio_component (c, vtype);
  2189. if (tail == NULL)
  2190. *cp = c;
  2191. else
  2192. tail->next = c;
  2193. tail = c;
  2194. }
  2195. }
  2196. mio_rparen ();
  2197. }
  2198. static void
  2199. mio_actual_arg (gfc_actual_arglist *a)
  2200. {
  2201. mio_lparen ();
  2202. mio_pool_string (&a->name);
  2203. mio_expr (&a->expr);
  2204. mio_rparen ();
  2205. }
  2206. static void
  2207. mio_actual_arglist (gfc_actual_arglist **ap)
  2208. {
  2209. gfc_actual_arglist *a, *tail;
  2210. mio_lparen ();
  2211. if (iomode == IO_OUTPUT)
  2212. {
  2213. for (a = *ap; a; a = a->next)
  2214. mio_actual_arg (a);
  2215. }
  2216. else
  2217. {
  2218. tail = NULL;
  2219. for (;;)
  2220. {
  2221. if (peek_atom () != ATOM_LPAREN)
  2222. break;
  2223. a = gfc_get_actual_arglist ();
  2224. if (tail == NULL)
  2225. *ap = a;
  2226. else
  2227. tail->next = a;
  2228. tail = a;
  2229. mio_actual_arg (a);
  2230. }
  2231. }
  2232. mio_rparen ();
  2233. }
  2234. /* Read and write formal argument lists. */
  2235. static void
  2236. mio_formal_arglist (gfc_formal_arglist **formal)
  2237. {
  2238. gfc_formal_arglist *f, *tail;
  2239. mio_lparen ();
  2240. if (iomode == IO_OUTPUT)
  2241. {
  2242. for (f = *formal; f; f = f->next)
  2243. mio_symbol_ref (&f->sym);
  2244. }
  2245. else
  2246. {
  2247. *formal = tail = NULL;
  2248. while (peek_atom () != ATOM_RPAREN)
  2249. {
  2250. f = gfc_get_formal_arglist ();
  2251. mio_symbol_ref (&f->sym);
  2252. if (*formal == NULL)
  2253. *formal = f;
  2254. else
  2255. tail->next = f;
  2256. tail = f;
  2257. }
  2258. }
  2259. mio_rparen ();
  2260. }
  2261. /* Save or restore a reference to a symbol node. */
  2262. pointer_info *
  2263. mio_symbol_ref (gfc_symbol **symp)
  2264. {
  2265. pointer_info *p;
  2266. p = mio_pointer_ref (symp);
  2267. if (p->type == P_UNKNOWN)
  2268. p->type = P_SYMBOL;
  2269. if (iomode == IO_OUTPUT)
  2270. {
  2271. if (p->u.wsym.state == UNREFERENCED)
  2272. p->u.wsym.state = NEEDS_WRITE;
  2273. }
  2274. else
  2275. {
  2276. if (p->u.rsym.state == UNUSED)
  2277. p->u.rsym.state = NEEDED;
  2278. }
  2279. return p;
  2280. }
  2281. /* Save or restore a reference to a symtree node. */
  2282. static void
  2283. mio_symtree_ref (gfc_symtree **stp)
  2284. {
  2285. pointer_info *p;
  2286. fixup_t *f;
  2287. if (iomode == IO_OUTPUT)
  2288. mio_symbol_ref (&(*stp)->n.sym);
  2289. else
  2290. {
  2291. require_atom (ATOM_INTEGER);
  2292. p = get_integer (atom_int);
  2293. /* An unused equivalence member; make a symbol and a symtree
  2294. for it. */
  2295. if (in_load_equiv && p->u.rsym.symtree == NULL)
  2296. {
  2297. /* Since this is not used, it must have a unique name. */
  2298. p->u.rsym.symtree = gfc_get_unique_symtree (gfc_current_ns);
  2299. /* Make the symbol. */
  2300. if (p->u.rsym.sym == NULL)
  2301. {
  2302. p->u.rsym.sym = gfc_new_symbol (p->u.rsym.true_name,
  2303. gfc_current_ns);
  2304. p->u.rsym.sym->module = gfc_get_string (p->u.rsym.module);
  2305. }
  2306. p->u.rsym.symtree->n.sym = p->u.rsym.sym;
  2307. p->u.rsym.symtree->n.sym->refs++;
  2308. p->u.rsym.referenced = 1;
  2309. /* If the symbol is PRIVATE and in COMMON, load_commons will
  2310. generate a fixup symbol, which must be associated. */
  2311. if (p->fixup)
  2312. resolve_fixups (p->fixup, p->u.rsym.sym);
  2313. p->fixup = NULL;
  2314. }
  2315. if (p->type == P_UNKNOWN)
  2316. p->type = P_SYMBOL;
  2317. if (p->u.rsym.state == UNUSED)
  2318. p->u.rsym.state = NEEDED;
  2319. if (p->u.rsym.symtree != NULL)
  2320. {
  2321. *stp = p->u.rsym.symtree;
  2322. }
  2323. else
  2324. {
  2325. f = XCNEW (fixup_t);
  2326. f->next = p->u.rsym.stfixup;
  2327. p->u.rsym.stfixup = f;
  2328. f->pointer = (void **) stp;
  2329. }
  2330. }
  2331. }
  2332. static void
  2333. mio_iterator (gfc_iterator **ip)
  2334. {
  2335. gfc_iterator *iter;
  2336. mio_lparen ();
  2337. if (iomode == IO_OUTPUT)
  2338. {
  2339. if (*ip == NULL)
  2340. goto done;
  2341. }
  2342. else
  2343. {
  2344. if (peek_atom () == ATOM_RPAREN)
  2345. {
  2346. *ip = NULL;
  2347. goto done;
  2348. }
  2349. *ip = gfc_get_iterator ();
  2350. }
  2351. iter = *ip;
  2352. mio_expr (&iter->var);
  2353. mio_expr (&iter->start);
  2354. mio_expr (&iter->end);
  2355. mio_expr (&iter->step);
  2356. done:
  2357. mio_rparen ();
  2358. }
  2359. static void
  2360. mio_constructor (gfc_constructor_base *cp)
  2361. {
  2362. gfc_constructor *c;
  2363. mio_lparen ();
  2364. if (iomode == IO_OUTPUT)
  2365. {
  2366. for (c = gfc_constructor_first (*cp); c; c = gfc_constructor_next (c))
  2367. {
  2368. mio_lparen ();
  2369. mio_expr (&c->expr);
  2370. mio_iterator (&c->iterator);
  2371. mio_rparen ();
  2372. }
  2373. }
  2374. else
  2375. {
  2376. while (peek_atom () != ATOM_RPAREN)
  2377. {
  2378. c = gfc_constructor_append_expr (cp, NULL, NULL);
  2379. mio_lparen ();
  2380. mio_expr (&c->expr);
  2381. mio_iterator (&c->iterator);
  2382. mio_rparen ();
  2383. }
  2384. }
  2385. mio_rparen ();
  2386. }
  2387. static const mstring ref_types[] = {
  2388. minit ("ARRAY", REF_ARRAY),
  2389. minit ("COMPONENT", REF_COMPONENT),
  2390. minit ("SUBSTRING", REF_SUBSTRING),
  2391. minit (NULL, -1)
  2392. };
  2393. static void
  2394. mio_ref (gfc_ref **rp)
  2395. {
  2396. gfc_ref *r;
  2397. mio_lparen ();
  2398. r = *rp;
  2399. r->type = MIO_NAME (ref_type) (r->type, ref_types);
  2400. switch (r->type)
  2401. {
  2402. case REF_ARRAY:
  2403. mio_array_ref (&r->u.ar);
  2404. break;
  2405. case REF_COMPONENT:
  2406. mio_symbol_ref (&r->u.c.sym);
  2407. mio_component_ref (&r->u.c.component);
  2408. break;
  2409. case REF_SUBSTRING:
  2410. mio_expr (&r->u.ss.start);
  2411. mio_expr (&r->u.ss.end);
  2412. mio_charlen (&r->u.ss.length);
  2413. break;
  2414. }
  2415. mio_rparen ();
  2416. }
  2417. static void
  2418. mio_ref_list (gfc_ref **rp)
  2419. {
  2420. gfc_ref *ref, *head, *tail;
  2421. mio_lparen ();
  2422. if (iomode == IO_OUTPUT)
  2423. {
  2424. for (ref = *rp; ref; ref = ref->next)
  2425. mio_ref (&ref);
  2426. }
  2427. else
  2428. {
  2429. head = tail = NULL;
  2430. while (peek_atom () != ATOM_RPAREN)
  2431. {
  2432. if (head == NULL)
  2433. head = tail = gfc_get_ref ();
  2434. else
  2435. {
  2436. tail->next = gfc_get_ref ();
  2437. tail = tail->next;
  2438. }
  2439. mio_ref (&tail);
  2440. }
  2441. *rp = head;
  2442. }
  2443. mio_rparen ();
  2444. }
  2445. /* Read and write an integer value. */
  2446. static void
  2447. mio_gmp_integer (mpz_t *integer)
  2448. {
  2449. char *p;
  2450. if (iomode == IO_INPUT)
  2451. {
  2452. if (parse_atom () != ATOM_STRING)
  2453. bad_module ("Expected integer string");
  2454. mpz_init (*integer);
  2455. if (mpz_set_str (*integer, atom_string, 10))
  2456. bad_module ("Error converting integer");
  2457. free (atom_string);
  2458. }
  2459. else
  2460. {
  2461. p = mpz_get_str (NULL, 10, *integer);
  2462. write_atom (ATOM_STRING, p);
  2463. free (p);
  2464. }
  2465. }
  2466. static void
  2467. mio_gmp_real (mpfr_t *real)
  2468. {
  2469. mp_exp_t exponent;
  2470. char *p;
  2471. if (iomode == IO_INPUT)
  2472. {
  2473. if (parse_atom () != ATOM_STRING)
  2474. bad_module ("Expected real string");
  2475. mpfr_init (*real);
  2476. mpfr_set_str (*real, atom_string, 16, GFC_RND_MODE);
  2477. free (atom_string);
  2478. }
  2479. else
  2480. {
  2481. p = mpfr_get_str (NULL, &exponent, 16, 0, *real, GFC_RND_MODE);
  2482. if (mpfr_nan_p (*real) || mpfr_inf_p (*real))
  2483. {
  2484. write_atom (ATOM_STRING, p);
  2485. free (p);
  2486. return;
  2487. }
  2488. atom_string = XCNEWVEC (char, strlen (p) + 20);
  2489. sprintf (atom_string, "0.%s@%ld", p, exponent);
  2490. /* Fix negative numbers. */
  2491. if (atom_string[2] == '-')
  2492. {
  2493. atom_string[0] = '-';
  2494. atom_string[1] = '0';
  2495. atom_string[2] = '.';
  2496. }
  2497. write_atom (ATOM_STRING, atom_string);
  2498. free (atom_string);
  2499. free (p);
  2500. }
  2501. }
  2502. /* Save and restore the shape of an array constructor. */
  2503. static void
  2504. mio_shape (mpz_t **pshape, int rank)
  2505. {
  2506. mpz_t *shape;
  2507. atom_type t;
  2508. int n;
  2509. /* A NULL shape is represented by (). */
  2510. mio_lparen ();
  2511. if (iomode == IO_OUTPUT)
  2512. {
  2513. shape = *pshape;
  2514. if (!shape)
  2515. {
  2516. mio_rparen ();
  2517. return;
  2518. }
  2519. }
  2520. else
  2521. {
  2522. t = peek_atom ();
  2523. if (t == ATOM_RPAREN)
  2524. {
  2525. *pshape = NULL;
  2526. mio_rparen ();
  2527. return;
  2528. }
  2529. shape = gfc_get_shape (rank);
  2530. *pshape = shape;
  2531. }
  2532. for (n = 0; n < rank; n++)
  2533. mio_gmp_integer (&shape[n]);
  2534. mio_rparen ();
  2535. }
  2536. static const mstring expr_types[] = {
  2537. minit ("OP", EXPR_OP),
  2538. minit ("FUNCTION", EXPR_FUNCTION),
  2539. minit ("CONSTANT", EXPR_CONSTANT),
  2540. minit ("VARIABLE", EXPR_VARIABLE),
  2541. minit ("SUBSTRING", EXPR_SUBSTRING),
  2542. minit ("STRUCTURE", EXPR_STRUCTURE),
  2543. minit ("ARRAY", EXPR_ARRAY),
  2544. minit ("NULL", EXPR_NULL),
  2545. minit ("COMPCALL", EXPR_COMPCALL),
  2546. minit (NULL, -1)
  2547. };
  2548. /* INTRINSIC_ASSIGN is missing because it is used as an index for
  2549. generic operators, not in expressions. INTRINSIC_USER is also
  2550. replaced by the correct function name by the time we see it. */
  2551. static const mstring intrinsics[] =
  2552. {
  2553. minit ("UPLUS", INTRINSIC_UPLUS),
  2554. minit ("UMINUS", INTRINSIC_UMINUS),
  2555. minit ("PLUS", INTRINSIC_PLUS),
  2556. minit ("MINUS", INTRINSIC_MINUS),
  2557. minit ("TIMES", INTRINSIC_TIMES),
  2558. minit ("DIVIDE", INTRINSIC_DIVIDE),
  2559. minit ("POWER", INTRINSIC_POWER),
  2560. minit ("CONCAT", INTRINSIC_CONCAT),
  2561. minit ("AND", INTRINSIC_AND),
  2562. minit ("OR", INTRINSIC_OR),
  2563. minit ("EQV", INTRINSIC_EQV),
  2564. minit ("NEQV", INTRINSIC_NEQV),
  2565. minit ("EQ_SIGN", INTRINSIC_EQ),
  2566. minit ("EQ", INTRINSIC_EQ_OS),
  2567. minit ("NE_SIGN", INTRINSIC_NE),
  2568. minit ("NE", INTRINSIC_NE_OS),
  2569. minit ("GT_SIGN", INTRINSIC_GT),
  2570. minit ("GT", INTRINSIC_GT_OS),
  2571. minit ("GE_SIGN", INTRINSIC_GE),
  2572. minit ("GE", INTRINSIC_GE_OS),
  2573. minit ("LT_SIGN", INTRINSIC_LT),
  2574. minit ("LT", INTRINSIC_LT_OS),
  2575. minit ("LE_SIGN", INTRINSIC_LE),
  2576. minit ("LE", INTRINSIC_LE_OS),
  2577. minit ("NOT", INTRINSIC_NOT),
  2578. minit ("PARENTHESES", INTRINSIC_PARENTHESES),
  2579. minit ("USER", INTRINSIC_USER),
  2580. minit (NULL, -1)
  2581. };
  2582. /* Remedy a couple of situations where the gfc_expr's can be defective. */
  2583. static void
  2584. fix_mio_expr (gfc_expr *e)
  2585. {
  2586. gfc_symtree *ns_st = NULL;
  2587. const char *fname;
  2588. if (iomode != IO_OUTPUT)
  2589. return;
  2590. if (e->symtree)
  2591. {
  2592. /* If this is a symtree for a symbol that came from a contained module
  2593. namespace, it has a unique name and we should look in the current
  2594. namespace to see if the required, non-contained symbol is available
  2595. yet. If so, the latter should be written. */
  2596. if (e->symtree->n.sym && check_unique_name (e->symtree->name))
  2597. {
  2598. const char *name = e->symtree->n.sym->name;
  2599. if (e->symtree->n.sym->attr.flavor == FL_DERIVED)
  2600. name = dt_upper_string (name);
  2601. ns_st = gfc_find_symtree (gfc_current_ns->sym_root, name);
  2602. }
  2603. /* On the other hand, if the existing symbol is the module name or the
  2604. new symbol is a dummy argument, do not do the promotion. */
  2605. if (ns_st && ns_st->n.sym
  2606. && ns_st->n.sym->attr.flavor != FL_MODULE
  2607. && !e->symtree->n.sym->attr.dummy)
  2608. e->symtree = ns_st;
  2609. }
  2610. else if (e->expr_type == EXPR_FUNCTION
  2611. && (e->value.function.name || e->value.function.isym))
  2612. {
  2613. gfc_symbol *sym;
  2614. /* In some circumstances, a function used in an initialization
  2615. expression, in one use associated module, can fail to be
  2616. coupled to its symtree when used in a specification
  2617. expression in another module. */
  2618. fname = e->value.function.esym ? e->value.function.esym->name
  2619. : e->value.function.isym->name;
  2620. e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
  2621. if (e->symtree)
  2622. return;
  2623. /* This is probably a reference to a private procedure from another
  2624. module. To prevent a segfault, make a generic with no specific
  2625. instances. If this module is used, without the required
  2626. specific coming from somewhere, the appropriate error message
  2627. is issued. */
  2628. gfc_get_symbol (fname, gfc_current_ns, &sym);
  2629. sym->attr.flavor = FL_PROCEDURE;
  2630. sym->attr.generic = 1;
  2631. e->symtree = gfc_find_symtree (gfc_current_ns->sym_root, fname);
  2632. gfc_commit_symbol (sym);
  2633. }
  2634. }
  2635. /* Read and write expressions. The form "()" is allowed to indicate a
  2636. NULL expression. */
  2637. static void
  2638. mio_expr (gfc_expr **ep)
  2639. {
  2640. gfc_expr *e;
  2641. atom_type t;
  2642. int flag;
  2643. mio_lparen ();
  2644. if (iomode == IO_OUTPUT)
  2645. {
  2646. if (*ep == NULL)
  2647. {
  2648. mio_rparen ();
  2649. return;
  2650. }
  2651. e = *ep;
  2652. MIO_NAME (expr_t) (e->expr_type, expr_types);
  2653. }
  2654. else
  2655. {
  2656. t = parse_atom ();
  2657. if (t == ATOM_RPAREN)
  2658. {
  2659. *ep = NULL;
  2660. return;
  2661. }
  2662. if (t != ATOM_NAME)
  2663. bad_module ("Expected expression type");
  2664. e = *ep = gfc_get_expr ();
  2665. e->where = gfc_current_locus;
  2666. e->expr_type = (expr_t) find_enum (expr_types);
  2667. }
  2668. mio_typespec (&e->ts);
  2669. mio_integer (&e->rank);
  2670. fix_mio_expr (e);
  2671. switch (e->expr_type)
  2672. {
  2673. case EXPR_OP:
  2674. e->value.op.op
  2675. = MIO_NAME (gfc_intrinsic_op) (e->value.op.op, intrinsics);
  2676. switch (e->value.op.op)
  2677. {
  2678. case INTRINSIC_UPLUS:
  2679. case INTRINSIC_UMINUS:
  2680. case INTRINSIC_NOT:
  2681. case INTRINSIC_PARENTHESES:
  2682. mio_expr (&e->value.op.op1);
  2683. break;
  2684. case INTRINSIC_PLUS:
  2685. case INTRINSIC_MINUS:
  2686. case INTRINSIC_TIMES:
  2687. case INTRINSIC_DIVIDE:
  2688. case INTRINSIC_POWER:
  2689. case INTRINSIC_CONCAT:
  2690. case INTRINSIC_AND:
  2691. case INTRINSIC_OR:
  2692. case INTRINSIC_EQV:
  2693. case INTRINSIC_NEQV:
  2694. case INTRINSIC_EQ:
  2695. case INTRINSIC_EQ_OS:
  2696. case INTRINSIC_NE:
  2697. case INTRINSIC_NE_OS:
  2698. case INTRINSIC_GT:
  2699. case INTRINSIC_GT_OS:
  2700. case INTRINSIC_GE:
  2701. case INTRINSIC_GE_OS:
  2702. case INTRINSIC_LT:
  2703. case INTRINSIC_LT_OS:
  2704. case INTRINSIC_LE:
  2705. case INTRINSIC_LE_OS:
  2706. mio_expr (&e->value.op.op1);
  2707. mio_expr (&e->value.op.op2);
  2708. break;
  2709. case INTRINSIC_USER:
  2710. /* INTRINSIC_USER should not appear in resolved expressions,
  2711. though for UDRs we need to stream unresolved ones. */
  2712. if (iomode == IO_OUTPUT)
  2713. write_atom (ATOM_STRING, e->value.op.uop->name);
  2714. else
  2715. {
  2716. char *name = read_string ();
  2717. const char *uop_name = find_use_name (name, true);
  2718. if (uop_name == NULL)
  2719. {
  2720. size_t len = strlen (name);
  2721. char *name2 = XCNEWVEC (char, len + 2);
  2722. memcpy (name2, name, len);
  2723. name2[len] = ' ';
  2724. name2[len + 1] = '\0';
  2725. free (name);
  2726. uop_name = name = name2;
  2727. }
  2728. e->value.op.uop = gfc_get_uop (uop_name);
  2729. free (name);
  2730. }
  2731. mio_expr (&e->value.op.op1);
  2732. mio_expr (&e->value.op.op2);
  2733. break;
  2734. default:
  2735. bad_module ("Bad operator");
  2736. }
  2737. break;
  2738. case EXPR_FUNCTION:
  2739. mio_symtree_ref (&e->symtree);
  2740. mio_actual_arglist (&e->value.function.actual);
  2741. if (iomode == IO_OUTPUT)
  2742. {
  2743. e->value.function.name
  2744. = mio_allocated_string (e->value.function.name);
  2745. if (e->value.function.esym)
  2746. flag = 1;
  2747. else if (e->ref)
  2748. flag = 2;
  2749. else if (e->value.function.isym == NULL)
  2750. flag = 3;
  2751. else
  2752. flag = 0;
  2753. mio_integer (&flag);
  2754. switch (flag)
  2755. {
  2756. case 1:
  2757. mio_symbol_ref (&e->value.function.esym);
  2758. break;
  2759. case 2:
  2760. mio_ref_list (&e->ref);
  2761. break;
  2762. case 3:
  2763. break;
  2764. default:
  2765. write_atom (ATOM_STRING, e->value.function.isym->name);
  2766. }
  2767. }
  2768. else
  2769. {
  2770. require_atom (ATOM_STRING);
  2771. if (atom_string[0] == '\0')
  2772. e->value.function.name = NULL;
  2773. else
  2774. e->value.function.name = gfc_get_string (atom_string);
  2775. free (atom_string);
  2776. mio_integer (&flag);
  2777. switch (flag)
  2778. {
  2779. case 1:
  2780. mio_symbol_ref (&e->value.function.esym);
  2781. break;
  2782. case 2:
  2783. mio_ref_list (&e->ref);
  2784. break;
  2785. case 3:
  2786. break;
  2787. default:
  2788. require_atom (ATOM_STRING);
  2789. e->value.function.isym = gfc_find_function (atom_string);
  2790. free (atom_string);
  2791. }
  2792. }
  2793. break;
  2794. case EXPR_VARIABLE:
  2795. mio_symtree_ref (&e->symtree);
  2796. mio_ref_list (&e->ref);
  2797. break;
  2798. case EXPR_SUBSTRING:
  2799. e->value.character.string
  2800. = CONST_CAST (gfc_char_t *,
  2801. mio_allocated_wide_string (e->value.character.string,
  2802. e->value.character.length));
  2803. mio_ref_list (&e->ref);
  2804. break;
  2805. case EXPR_STRUCTURE:
  2806. case EXPR_ARRAY:
  2807. mio_constructor (&e->value.constructor);
  2808. mio_shape (&e->shape, e->rank);
  2809. break;
  2810. case EXPR_CONSTANT:
  2811. switch (e->ts.type)
  2812. {
  2813. case BT_INTEGER:
  2814. mio_gmp_integer (&e->value.integer);
  2815. break;
  2816. case BT_REAL:
  2817. gfc_set_model_kind (e->ts.kind);
  2818. mio_gmp_real (&e->value.real);
  2819. break;
  2820. case BT_COMPLEX:
  2821. gfc_set_model_kind (e->ts.kind);
  2822. mio_gmp_real (&mpc_realref (e->value.complex));
  2823. mio_gmp_real (&mpc_imagref (e->value.complex));
  2824. break;
  2825. case BT_LOGICAL:
  2826. mio_integer (&e->value.logical);
  2827. break;
  2828. case BT_CHARACTER:
  2829. mio_integer (&e->value.character.length);
  2830. e->value.character.string
  2831. = CONST_CAST (gfc_char_t *,
  2832. mio_allocated_wide_string (e->value.character.string,
  2833. e->value.character.length));
  2834. break;
  2835. default:
  2836. bad_module ("Bad type in constant expression");
  2837. }
  2838. break;
  2839. case EXPR_NULL:
  2840. break;
  2841. case EXPR_COMPCALL:
  2842. case EXPR_PPC:
  2843. gcc_unreachable ();
  2844. break;
  2845. }
  2846. mio_rparen ();
  2847. }
  2848. /* Read and write namelists. */
  2849. static void
  2850. mio_namelist (gfc_symbol *sym)
  2851. {
  2852. gfc_namelist *n, *m;
  2853. const char *check_name;
  2854. mio_lparen ();
  2855. if (iomode == IO_OUTPUT)
  2856. {
  2857. for (n = sym->namelist; n; n = n->next)
  2858. mio_symbol_ref (&n->sym);
  2859. }
  2860. else
  2861. {
  2862. /* This departure from the standard is flagged as an error.
  2863. It does, in fact, work correctly. TODO: Allow it
  2864. conditionally? */
  2865. if (sym->attr.flavor == FL_NAMELIST)
  2866. {
  2867. check_name = find_use_name (sym->name, false);
  2868. if (check_name && strcmp (check_name, sym->name) != 0)
  2869. gfc_error ("Namelist %s cannot be renamed by USE "
  2870. "association to %s", sym->name, check_name);
  2871. }
  2872. m = NULL;
  2873. while (peek_atom () != ATOM_RPAREN)
  2874. {
  2875. n = gfc_get_namelist ();
  2876. mio_symbol_ref (&n->sym);
  2877. if (sym->namelist == NULL)
  2878. sym->namelist = n;
  2879. else
  2880. m->next = n;
  2881. m = n;
  2882. }
  2883. sym->namelist_tail = m;
  2884. }
  2885. mio_rparen ();
  2886. }
  2887. /* Save/restore lists of gfc_interface structures. When loading an
  2888. interface, we are really appending to the existing list of
  2889. interfaces. Checking for duplicate and ambiguous interfaces has to
  2890. be done later when all symbols have been loaded. */
  2891. pointer_info *
  2892. mio_interface_rest (gfc_interface **ip)
  2893. {
  2894. gfc_interface *tail, *p;
  2895. pointer_info *pi = NULL;
  2896. if (iomode == IO_OUTPUT)
  2897. {
  2898. if (ip != NULL)
  2899. for (p = *ip; p; p = p->next)
  2900. mio_symbol_ref (&p->sym);
  2901. }
  2902. else
  2903. {
  2904. if (*ip == NULL)
  2905. tail = NULL;
  2906. else
  2907. {
  2908. tail = *ip;
  2909. while (tail->next)
  2910. tail = tail->next;
  2911. }
  2912. for (;;)
  2913. {
  2914. if (peek_atom () == ATOM_RPAREN)
  2915. break;
  2916. p = gfc_get_interface ();
  2917. p->where = gfc_current_locus;
  2918. pi = mio_symbol_ref (&p->sym);
  2919. if (tail == NULL)
  2920. *ip = p;
  2921. else
  2922. tail->next = p;
  2923. tail = p;
  2924. }
  2925. }
  2926. mio_rparen ();
  2927. return pi;
  2928. }
  2929. /* Save/restore a nameless operator interface. */
  2930. static void
  2931. mio_interface (gfc_interface **ip)
  2932. {
  2933. mio_lparen ();
  2934. mio_interface_rest (ip);
  2935. }
  2936. /* Save/restore a named operator interface. */
  2937. static void
  2938. mio_symbol_interface (const char **name, const char **module,
  2939. gfc_interface **ip)
  2940. {
  2941. mio_lparen ();
  2942. mio_pool_string (name);
  2943. mio_pool_string (module);
  2944. mio_interface_rest (ip);
  2945. }
  2946. static void
  2947. mio_namespace_ref (gfc_namespace **nsp)
  2948. {
  2949. gfc_namespace *ns;
  2950. pointer_info *p;
  2951. p = mio_pointer_ref (nsp);
  2952. if (p->type == P_UNKNOWN)
  2953. p->type = P_NAMESPACE;
  2954. if (iomode == IO_INPUT && p->integer != 0)
  2955. {
  2956. ns = (gfc_namespace *) p->u.pointer;
  2957. if (ns == NULL)
  2958. {
  2959. ns = gfc_get_namespace (NULL, 0);
  2960. associate_integer_pointer (p, ns);
  2961. }
  2962. else
  2963. ns->refs++;
  2964. }
  2965. }
  2966. /* Save/restore the f2k_derived namespace of a derived-type symbol. */
  2967. static gfc_namespace* current_f2k_derived;
  2968. static void
  2969. mio_typebound_proc (gfc_typebound_proc** proc)
  2970. {
  2971. int flag;
  2972. int overriding_flag;
  2973. if (iomode == IO_INPUT)
  2974. {
  2975. *proc = gfc_get_typebound_proc (NULL);
  2976. (*proc)->where = gfc_current_locus;
  2977. }
  2978. gcc_assert (*proc);
  2979. mio_lparen ();
  2980. (*proc)->access = MIO_NAME (gfc_access) ((*proc)->access, access_types);
  2981. /* IO the NON_OVERRIDABLE/DEFERRED combination. */
  2982. gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
  2983. overriding_flag = ((*proc)->deferred << 1) | (*proc)->non_overridable;
  2984. overriding_flag = mio_name (overriding_flag, binding_overriding);
  2985. (*proc)->deferred = ((overriding_flag & 2) != 0);
  2986. (*proc)->non_overridable = ((overriding_flag & 1) != 0);
  2987. gcc_assert (!((*proc)->deferred && (*proc)->non_overridable));
  2988. (*proc)->nopass = mio_name ((*proc)->nopass, binding_passing);
  2989. (*proc)->is_generic = mio_name ((*proc)->is_generic, binding_generic);
  2990. (*proc)->ppc = mio_name((*proc)->ppc, binding_ppc);
  2991. mio_pool_string (&((*proc)->pass_arg));
  2992. flag = (int) (*proc)->pass_arg_num;
  2993. mio_integer (&flag);
  2994. (*proc)->pass_arg_num = (unsigned) flag;
  2995. if ((*proc)->is_generic)
  2996. {
  2997. gfc_tbp_generic* g;
  2998. int iop;
  2999. mio_lparen ();
  3000. if (iomode == IO_OUTPUT)
  3001. for (g = (*proc)->u.generic; g; g = g->next)
  3002. {
  3003. iop = (int) g->is_operator;
  3004. mio_integer (&iop);
  3005. mio_allocated_string (g->specific_st->name);
  3006. }
  3007. else
  3008. {
  3009. (*proc)->u.generic = NULL;
  3010. while (peek_atom () != ATOM_RPAREN)
  3011. {
  3012. gfc_symtree** sym_root;
  3013. g = gfc_get_tbp_generic ();
  3014. g->specific = NULL;
  3015. mio_integer (&iop);
  3016. g->is_operator = (bool) iop;
  3017. require_atom (ATOM_STRING);
  3018. sym_root = &current_f2k_derived->tb_sym_root;
  3019. g->specific_st = gfc_get_tbp_symtree (sym_root, atom_string);
  3020. free (atom_string);
  3021. g->next = (*proc)->u.generic;
  3022. (*proc)->u.generic = g;
  3023. }
  3024. }
  3025. mio_rparen ();
  3026. }
  3027. else if (!(*proc)->ppc)
  3028. mio_symtree_ref (&(*proc)->u.specific);
  3029. mio_rparen ();
  3030. }
  3031. /* Walker-callback function for this purpose. */
  3032. static void
  3033. mio_typebound_symtree (gfc_symtree* st)
  3034. {
  3035. if (iomode == IO_OUTPUT && !st->n.tb)
  3036. return;
  3037. if (iomode == IO_OUTPUT)
  3038. {
  3039. mio_lparen ();
  3040. mio_allocated_string (st->name);
  3041. }
  3042. /* For IO_INPUT, the above is done in mio_f2k_derived. */
  3043. mio_typebound_proc (&st->n.tb);
  3044. mio_rparen ();
  3045. }
  3046. /* IO a full symtree (in all depth). */
  3047. static void
  3048. mio_full_typebound_tree (gfc_symtree** root)
  3049. {
  3050. mio_lparen ();
  3051. if (iomode == IO_OUTPUT)
  3052. gfc_traverse_symtree (*root, &mio_typebound_symtree);
  3053. else
  3054. {
  3055. while (peek_atom () == ATOM_LPAREN)
  3056. {
  3057. gfc_symtree* st;
  3058. mio_lparen ();
  3059. require_atom (ATOM_STRING);
  3060. st = gfc_get_tbp_symtree (root, atom_string);
  3061. free (atom_string);
  3062. mio_typebound_symtree (st);
  3063. }
  3064. }
  3065. mio_rparen ();
  3066. }
  3067. static void
  3068. mio_finalizer (gfc_finalizer **f)
  3069. {
  3070. if (iomode == IO_OUTPUT)
  3071. {
  3072. gcc_assert (*f);
  3073. gcc_assert ((*f)->proc_tree); /* Should already be resolved. */
  3074. mio_symtree_ref (&(*f)->proc_tree);
  3075. }
  3076. else
  3077. {
  3078. *f = gfc_get_finalizer ();
  3079. (*f)->where = gfc_current_locus; /* Value should not matter. */
  3080. (*f)->next = NULL;
  3081. mio_symtree_ref (&(*f)->proc_tree);
  3082. (*f)->proc_sym = NULL;
  3083. }
  3084. }
  3085. static void
  3086. mio_f2k_derived (gfc_namespace *f2k)
  3087. {
  3088. current_f2k_derived = f2k;
  3089. /* Handle the list of finalizer procedures. */
  3090. mio_lparen ();
  3091. if (iomode == IO_OUTPUT)
  3092. {
  3093. gfc_finalizer *f;
  3094. for (f = f2k->finalizers; f; f = f->next)
  3095. mio_finalizer (&f);
  3096. }
  3097. else
  3098. {
  3099. f2k->finalizers = NULL;
  3100. while (peek_atom () != ATOM_RPAREN)
  3101. {
  3102. gfc_finalizer *cur = NULL;
  3103. mio_finalizer (&cur);
  3104. cur->next = f2k->finalizers;
  3105. f2k->finalizers = cur;
  3106. }
  3107. }
  3108. mio_rparen ();
  3109. /* Handle type-bound procedures. */
  3110. mio_full_typebound_tree (&f2k->tb_sym_root);
  3111. /* Type-bound user operators. */
  3112. mio_full_typebound_tree (&f2k->tb_uop_root);
  3113. /* Type-bound intrinsic operators. */
  3114. mio_lparen ();
  3115. if (iomode == IO_OUTPUT)
  3116. {
  3117. int op;
  3118. for (op = GFC_INTRINSIC_BEGIN; op != GFC_INTRINSIC_END; ++op)
  3119. {
  3120. gfc_intrinsic_op realop;
  3121. if (op == INTRINSIC_USER || !f2k->tb_op[op])
  3122. continue;
  3123. mio_lparen ();
  3124. realop = (gfc_intrinsic_op) op;
  3125. mio_intrinsic_op (&realop);
  3126. mio_typebound_proc (&f2k->tb_op[op]);
  3127. mio_rparen ();
  3128. }
  3129. }
  3130. else
  3131. while (peek_atom () != ATOM_RPAREN)
  3132. {
  3133. gfc_intrinsic_op op = GFC_INTRINSIC_BEGIN; /* Silence GCC. */
  3134. mio_lparen ();
  3135. mio_intrinsic_op (&op);
  3136. mio_typebound_proc (&f2k->tb_op[op]);
  3137. mio_rparen ();
  3138. }
  3139. mio_rparen ();
  3140. }
  3141. static void
  3142. mio_full_f2k_derived (gfc_symbol *sym)
  3143. {
  3144. mio_lparen ();
  3145. if (iomode == IO_OUTPUT)
  3146. {
  3147. if (sym->f2k_derived)
  3148. mio_f2k_derived (sym->f2k_derived);
  3149. }
  3150. else
  3151. {
  3152. if (peek_atom () != ATOM_RPAREN)
  3153. {
  3154. sym->f2k_derived = gfc_get_namespace (NULL, 0);
  3155. mio_f2k_derived (sym->f2k_derived);
  3156. }
  3157. else
  3158. gcc_assert (!sym->f2k_derived);
  3159. }
  3160. mio_rparen ();
  3161. }
  3162. static const mstring omp_declare_simd_clauses[] =
  3163. {
  3164. minit ("INBRANCH", 0),
  3165. minit ("NOTINBRANCH", 1),
  3166. minit ("SIMDLEN", 2),
  3167. minit ("UNIFORM", 3),
  3168. minit ("LINEAR", 4),
  3169. minit ("ALIGNED", 5),
  3170. minit (NULL, -1)
  3171. };
  3172. /* Handle !$omp declare simd. */
  3173. static void
  3174. mio_omp_declare_simd (gfc_namespace *ns, gfc_omp_declare_simd **odsp)
  3175. {
  3176. if (iomode == IO_OUTPUT)
  3177. {
  3178. if (*odsp == NULL)
  3179. return;
  3180. }
  3181. else if (peek_atom () != ATOM_LPAREN)
  3182. return;
  3183. gfc_omp_declare_simd *ods = *odsp;
  3184. mio_lparen ();
  3185. if (iomode == IO_OUTPUT)
  3186. {
  3187. write_atom (ATOM_NAME, "OMP_DECLARE_SIMD");
  3188. if (ods->clauses)
  3189. {
  3190. gfc_omp_namelist *n;
  3191. if (ods->clauses->inbranch)
  3192. mio_name (0, omp_declare_simd_clauses);
  3193. if (ods->clauses->notinbranch)
  3194. mio_name (1, omp_declare_simd_clauses);
  3195. if (ods->clauses->simdlen_expr)
  3196. {
  3197. mio_name (2, omp_declare_simd_clauses);
  3198. mio_expr (&ods->clauses->simdlen_expr);
  3199. }
  3200. for (n = ods->clauses->lists[OMP_LIST_UNIFORM]; n; n = n->next)
  3201. {
  3202. mio_name (3, omp_declare_simd_clauses);
  3203. mio_symbol_ref (&n->sym);
  3204. }
  3205. for (n = ods->clauses->lists[OMP_LIST_LINEAR]; n; n = n->next)
  3206. {
  3207. mio_name (4, omp_declare_simd_clauses);
  3208. mio_symbol_ref (&n->sym);
  3209. mio_expr (&n->expr);
  3210. }
  3211. for (n = ods->clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
  3212. {
  3213. mio_name (5, omp_declare_simd_clauses);
  3214. mio_symbol_ref (&n->sym);
  3215. mio_expr (&n->expr);
  3216. }
  3217. }
  3218. }
  3219. else
  3220. {
  3221. gfc_omp_namelist **ptrs[3] = { NULL, NULL, NULL };
  3222. require_atom (ATOM_NAME);
  3223. *odsp = ods = gfc_get_omp_declare_simd ();
  3224. ods->where = gfc_current_locus;
  3225. ods->proc_name = ns->proc_name;
  3226. if (peek_atom () == ATOM_NAME)
  3227. {
  3228. ods->clauses = gfc_get_omp_clauses ();
  3229. ptrs[0] = &ods->clauses->lists[OMP_LIST_UNIFORM];
  3230. ptrs[1] = &ods->clauses->lists[OMP_LIST_LINEAR];
  3231. ptrs[2] = &ods->clauses->lists[OMP_LIST_ALIGNED];
  3232. }
  3233. while (peek_atom () == ATOM_NAME)
  3234. {
  3235. gfc_omp_namelist *n;
  3236. int t = mio_name (0, omp_declare_simd_clauses);
  3237. switch (t)
  3238. {
  3239. case 0: ods->clauses->inbranch = true; break;
  3240. case 1: ods->clauses->notinbranch = true; break;
  3241. case 2: mio_expr (&ods->clauses->simdlen_expr); break;
  3242. case 3:
  3243. case 4:
  3244. case 5:
  3245. *ptrs[t - 3] = n = gfc_get_omp_namelist ();
  3246. ptrs[t - 3] = &n->next;
  3247. mio_symbol_ref (&n->sym);
  3248. if (t != 3)
  3249. mio_expr (&n->expr);
  3250. break;
  3251. }
  3252. }
  3253. }
  3254. mio_omp_declare_simd (ns, &ods->next);
  3255. mio_rparen ();
  3256. }
  3257. static const mstring omp_declare_reduction_stmt[] =
  3258. {
  3259. minit ("ASSIGN", 0),
  3260. minit ("CALL", 1),
  3261. minit (NULL, -1)
  3262. };
  3263. static void
  3264. mio_omp_udr_expr (gfc_omp_udr *udr, gfc_symbol **sym1, gfc_symbol **sym2,
  3265. gfc_namespace *ns, bool is_initializer)
  3266. {
  3267. if (iomode == IO_OUTPUT)
  3268. {
  3269. if ((*sym1)->module == NULL)
  3270. {
  3271. (*sym1)->module = module_name;
  3272. (*sym2)->module = module_name;
  3273. }
  3274. mio_symbol_ref (sym1);
  3275. mio_symbol_ref (sym2);
  3276. if (ns->code->op == EXEC_ASSIGN)
  3277. {
  3278. mio_name (0, omp_declare_reduction_stmt);
  3279. mio_expr (&ns->code->expr1);
  3280. mio_expr (&ns->code->expr2);
  3281. }
  3282. else
  3283. {
  3284. int flag;
  3285. mio_name (1, omp_declare_reduction_stmt);
  3286. mio_symtree_ref (&ns->code->symtree);
  3287. mio_actual_arglist (&ns->code->ext.actual);
  3288. flag = ns->code->resolved_isym != NULL;
  3289. mio_integer (&flag);
  3290. if (flag)
  3291. write_atom (ATOM_STRING, ns->code->resolved_isym->name);
  3292. else
  3293. mio_symbol_ref (&ns->code->resolved_sym);
  3294. }
  3295. }
  3296. else
  3297. {
  3298. pointer_info *p1 = mio_symbol_ref (sym1);
  3299. pointer_info *p2 = mio_symbol_ref (sym2);
  3300. gfc_symbol *sym;
  3301. gcc_assert (p1->u.rsym.ns == p2->u.rsym.ns);
  3302. gcc_assert (p1->u.rsym.sym == NULL);
  3303. /* Add hidden symbols to the symtree. */
  3304. pointer_info *q = get_integer (p1->u.rsym.ns);
  3305. q->u.pointer = (void *) ns;
  3306. sym = gfc_new_symbol (is_initializer ? "omp_priv" : "omp_out", ns);
  3307. sym->ts = udr->ts;
  3308. sym->module = gfc_get_string (p1->u.rsym.module);
  3309. associate_integer_pointer (p1, sym);
  3310. sym->attr.omp_udr_artificial_var = 1;
  3311. gcc_assert (p2->u.rsym.sym == NULL);
  3312. sym = gfc_new_symbol (is_initializer ? "omp_orig" : "omp_in", ns);
  3313. sym->ts = udr->ts;
  3314. sym->module = gfc_get_string (p2->u.rsym.module);
  3315. associate_integer_pointer (p2, sym);
  3316. sym->attr.omp_udr_artificial_var = 1;
  3317. if (mio_name (0, omp_declare_reduction_stmt) == 0)
  3318. {
  3319. ns->code = gfc_get_code (EXEC_ASSIGN);
  3320. mio_expr (&ns->code->expr1);
  3321. mio_expr (&ns->code->expr2);
  3322. }
  3323. else
  3324. {
  3325. int flag;
  3326. ns->code = gfc_get_code (EXEC_CALL);
  3327. mio_symtree_ref (&ns->code->symtree);
  3328. mio_actual_arglist (&ns->code->ext.actual);
  3329. mio_integer (&flag);
  3330. if (flag)
  3331. {
  3332. require_atom (ATOM_STRING);
  3333. ns->code->resolved_isym = gfc_find_subroutine (atom_string);
  3334. free (atom_string);
  3335. }
  3336. else
  3337. mio_symbol_ref (&ns->code->resolved_sym);
  3338. }
  3339. ns->code->loc = gfc_current_locus;
  3340. ns->omp_udr_ns = 1;
  3341. }
  3342. }
  3343. /* Unlike most other routines, the address of the symbol node is already
  3344. fixed on input and the name/module has already been filled in.
  3345. If you update the symbol format here, don't forget to update read_module
  3346. as well (look for "seek to the symbol's component list"). */
  3347. static void
  3348. mio_symbol (gfc_symbol *sym)
  3349. {
  3350. int intmod = INTMOD_NONE;
  3351. mio_lparen ();
  3352. mio_symbol_attribute (&sym->attr);
  3353. /* Note that components are always saved, even if they are supposed
  3354. to be private. Component access is checked during searching. */
  3355. mio_component_list (&sym->components, sym->attr.vtype);
  3356. if (sym->components != NULL)
  3357. sym->component_access
  3358. = MIO_NAME (gfc_access) (sym->component_access, access_types);
  3359. mio_typespec (&sym->ts);
  3360. if (sym->ts.type == BT_CLASS)
  3361. sym->attr.class_ok = 1;
  3362. if (iomode == IO_OUTPUT)
  3363. mio_namespace_ref (&sym->formal_ns);
  3364. else
  3365. {
  3366. mio_namespace_ref (&sym->formal_ns);
  3367. if (sym->formal_ns)
  3368. sym->formal_ns->proc_name = sym;
  3369. }
  3370. /* Save/restore common block links. */
  3371. mio_symbol_ref (&sym->common_next);
  3372. mio_formal_arglist (&sym->formal);
  3373. if (sym->attr.flavor == FL_PARAMETER)
  3374. mio_expr (&sym->value);
  3375. mio_array_spec (&sym->as);
  3376. mio_symbol_ref (&sym->result);
  3377. if (sym->attr.cray_pointee)
  3378. mio_symbol_ref (&sym->cp_pointer);
  3379. /* Load/save the f2k_derived namespace of a derived-type symbol. */
  3380. mio_full_f2k_derived (sym);
  3381. mio_namelist (sym);
  3382. /* Add the fields that say whether this is from an intrinsic module,
  3383. and if so, what symbol it is within the module. */
  3384. /* mio_integer (&(sym->from_intmod)); */
  3385. if (iomode == IO_OUTPUT)
  3386. {
  3387. intmod = sym->from_intmod;
  3388. mio_integer (&intmod);
  3389. }
  3390. else
  3391. {
  3392. mio_integer (&intmod);
  3393. if (current_intmod)
  3394. sym->from_intmod = current_intmod;
  3395. else
  3396. sym->from_intmod = (intmod_id) intmod;
  3397. }
  3398. mio_integer (&(sym->intmod_sym_id));
  3399. if (sym->attr.flavor == FL_DERIVED)
  3400. mio_integer (&(sym->hash_value));
  3401. if (sym->formal_ns
  3402. && sym->formal_ns->proc_name == sym
  3403. && sym->formal_ns->entries == NULL)
  3404. mio_omp_declare_simd (sym->formal_ns, &sym->formal_ns->omp_declare_simd);
  3405. mio_rparen ();
  3406. }
  3407. /************************* Top level subroutines *************************/
  3408. /* Given a root symtree node and a symbol, try to find a symtree that
  3409. references the symbol that is not a unique name. */
  3410. static gfc_symtree *
  3411. find_symtree_for_symbol (gfc_symtree *st, gfc_symbol *sym)
  3412. {
  3413. gfc_symtree *s = NULL;
  3414. if (st == NULL)
  3415. return s;
  3416. s = find_symtree_for_symbol (st->right, sym);
  3417. if (s != NULL)
  3418. return s;
  3419. s = find_symtree_for_symbol (st->left, sym);
  3420. if (s != NULL)
  3421. return s;
  3422. if (st->n.sym == sym && !check_unique_name (st->name))
  3423. return st;
  3424. return s;
  3425. }
  3426. /* A recursive function to look for a specific symbol by name and by
  3427. module. Whilst several symtrees might point to one symbol, its
  3428. is sufficient for the purposes here than one exist. Note that
  3429. generic interfaces are distinguished as are symbols that have been
  3430. renamed in another module. */
  3431. static gfc_symtree *
  3432. find_symbol (gfc_symtree *st, const char *name,
  3433. const char *module, int generic)
  3434. {
  3435. int c;
  3436. gfc_symtree *retval, *s;
  3437. if (st == NULL || st->n.sym == NULL)
  3438. return NULL;
  3439. c = strcmp (name, st->n.sym->name);
  3440. if (c == 0 && st->n.sym->module
  3441. && strcmp (module, st->n.sym->module) == 0
  3442. && !check_unique_name (st->name))
  3443. {
  3444. s = gfc_find_symtree (gfc_current_ns->sym_root, name);
  3445. /* Detect symbols that are renamed by use association in another
  3446. module by the absence of a symtree and null attr.use_rename,
  3447. since the latter is not transmitted in the module file. */
  3448. if (((!generic && !st->n.sym->attr.generic)
  3449. || (generic && st->n.sym->attr.generic))
  3450. && !(s == NULL && !st->n.sym->attr.use_rename))
  3451. return st;
  3452. }
  3453. retval = find_symbol (st->left, name, module, generic);
  3454. if (retval == NULL)
  3455. retval = find_symbol (st->right, name, module, generic);
  3456. return retval;
  3457. }
  3458. /* Skip a list between balanced left and right parens.
  3459. By setting NEST_LEVEL one assumes that a number of NEST_LEVEL opening parens
  3460. have been already parsed by hand, and the remaining of the content is to be
  3461. skipped here. The default value is 0 (balanced parens). */
  3462. static void
  3463. skip_list (int nest_level = 0)
  3464. {
  3465. int level;
  3466. level = nest_level;
  3467. do
  3468. {
  3469. switch (parse_atom ())
  3470. {
  3471. case ATOM_LPAREN:
  3472. level++;
  3473. break;
  3474. case ATOM_RPAREN:
  3475. level--;
  3476. break;
  3477. case ATOM_STRING:
  3478. free (atom_string);
  3479. break;
  3480. case ATOM_NAME:
  3481. case ATOM_INTEGER:
  3482. break;
  3483. }
  3484. }
  3485. while (level > 0);
  3486. }
  3487. /* Load operator interfaces from the module. Interfaces are unusual
  3488. in that they attach themselves to existing symbols. */
  3489. static void
  3490. load_operator_interfaces (void)
  3491. {
  3492. const char *p;
  3493. char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
  3494. gfc_user_op *uop;
  3495. pointer_info *pi = NULL;
  3496. int n, i;
  3497. mio_lparen ();
  3498. while (peek_atom () != ATOM_RPAREN)
  3499. {
  3500. mio_lparen ();
  3501. mio_internal_string (name);
  3502. mio_internal_string (module);
  3503. n = number_use_names (name, true);
  3504. n = n ? n : 1;
  3505. for (i = 1; i <= n; i++)
  3506. {
  3507. /* Decide if we need to load this one or not. */
  3508. p = find_use_name_n (name, &i, true);
  3509. if (p == NULL)
  3510. {
  3511. while (parse_atom () != ATOM_RPAREN);
  3512. continue;
  3513. }
  3514. if (i == 1)
  3515. {
  3516. uop = gfc_get_uop (p);
  3517. pi = mio_interface_rest (&uop->op);
  3518. }
  3519. else
  3520. {
  3521. if (gfc_find_uop (p, NULL))
  3522. continue;
  3523. uop = gfc_get_uop (p);
  3524. uop->op = gfc_get_interface ();
  3525. uop->op->where = gfc_current_locus;
  3526. add_fixup (pi->integer, &uop->op->sym);
  3527. }
  3528. }
  3529. }
  3530. mio_rparen ();
  3531. }
  3532. /* Load interfaces from the module. Interfaces are unusual in that
  3533. they attach themselves to existing symbols. */
  3534. static void
  3535. load_generic_interfaces (void)
  3536. {
  3537. const char *p;
  3538. char name[GFC_MAX_SYMBOL_LEN + 1], module[GFC_MAX_SYMBOL_LEN + 1];
  3539. gfc_symbol *sym;
  3540. gfc_interface *generic = NULL, *gen = NULL;
  3541. int n, i, renamed;
  3542. bool ambiguous_set = false;
  3543. mio_lparen ();
  3544. while (peek_atom () != ATOM_RPAREN)
  3545. {
  3546. mio_lparen ();
  3547. mio_internal_string (name);
  3548. mio_internal_string (module);
  3549. n = number_use_names (name, false);
  3550. renamed = n ? 1 : 0;
  3551. n = n ? n : 1;
  3552. for (i = 1; i <= n; i++)
  3553. {
  3554. gfc_symtree *st;
  3555. /* Decide if we need to load this one or not. */
  3556. p = find_use_name_n (name, &i, false);
  3557. st = find_symbol (gfc_current_ns->sym_root,
  3558. name, module_name, 1);
  3559. if (!p || gfc_find_symbol (p, NULL, 0, &sym))
  3560. {
  3561. /* Skip the specific names for these cases. */
  3562. while (i == 1 && parse_atom () != ATOM_RPAREN);
  3563. continue;
  3564. }
  3565. /* If the symbol exists already and is being USEd without being
  3566. in an ONLY clause, do not load a new symtree(11.3.2). */
  3567. if (!only_flag && st)
  3568. sym = st->n.sym;
  3569. if (!sym)
  3570. {
  3571. if (st)
  3572. {
  3573. sym = st->n.sym;
  3574. if (strcmp (st->name, p) != 0)
  3575. {
  3576. st = gfc_new_symtree (&gfc_current_ns->sym_root, p);
  3577. st->n.sym = sym;
  3578. sym->refs++;
  3579. }
  3580. }
  3581. /* Since we haven't found a valid generic interface, we had
  3582. better make one. */
  3583. if (!sym)
  3584. {
  3585. gfc_get_symbol (p, NULL, &sym);
  3586. sym->name = gfc_get_string (name);
  3587. sym->module = module_name;
  3588. sym->attr.flavor = FL_PROCEDURE;
  3589. sym->attr.generic = 1;
  3590. sym->attr.use_assoc = 1;
  3591. }
  3592. }
  3593. else
  3594. {
  3595. /* Unless sym is a generic interface, this reference
  3596. is ambiguous. */
  3597. if (st == NULL)
  3598. st = gfc_find_symtree (gfc_current_ns->sym_root, p);
  3599. sym = st->n.sym;
  3600. if (st && !sym->attr.generic
  3601. && !st->ambiguous
  3602. && sym->module
  3603. && strcmp (module, sym->module))
  3604. {
  3605. ambiguous_set = true;
  3606. st->ambiguous = 1;
  3607. }
  3608. }
  3609. sym->attr.use_only = only_flag;
  3610. sym->attr.use_rename = renamed;
  3611. if (i == 1)
  3612. {
  3613. mio_interface_rest (&sym->generic);
  3614. generic = sym->generic;
  3615. }
  3616. else if (!sym->generic)
  3617. {
  3618. sym->generic = generic;
  3619. sym->attr.generic_copy = 1;
  3620. }
  3621. /* If a procedure that is not generic has generic interfaces
  3622. that include itself, it is generic! We need to take care
  3623. to retain symbols ambiguous that were already so. */
  3624. if (sym->attr.use_assoc
  3625. && !sym->attr.generic
  3626. && sym->attr.flavor == FL_PROCEDURE)
  3627. {
  3628. for (gen = generic; gen; gen = gen->next)
  3629. {
  3630. if (gen->sym == sym)
  3631. {
  3632. sym->attr.generic = 1;
  3633. if (ambiguous_set)
  3634. st->ambiguous = 0;
  3635. break;
  3636. }
  3637. }
  3638. }
  3639. }
  3640. }
  3641. mio_rparen ();
  3642. }
  3643. /* Load common blocks. */
  3644. static void
  3645. load_commons (void)
  3646. {
  3647. char name[GFC_MAX_SYMBOL_LEN + 1];
  3648. gfc_common_head *p;
  3649. mio_lparen ();
  3650. while (peek_atom () != ATOM_RPAREN)
  3651. {
  3652. int flags;
  3653. char* label;
  3654. mio_lparen ();
  3655. mio_internal_string (name);
  3656. p = gfc_get_common (name, 1);
  3657. mio_symbol_ref (&p->head);
  3658. mio_integer (&flags);
  3659. if (flags & 1)
  3660. p->saved = 1;
  3661. if (flags & 2)
  3662. p->threadprivate = 1;
  3663. p->use_assoc = 1;
  3664. /* Get whether this was a bind(c) common or not. */
  3665. mio_integer (&p->is_bind_c);
  3666. /* Get the binding label. */
  3667. label = read_string ();
  3668. if (strlen (label))
  3669. p->binding_label = IDENTIFIER_POINTER (get_identifier (label));
  3670. XDELETEVEC (label);
  3671. mio_rparen ();
  3672. }
  3673. mio_rparen ();
  3674. }
  3675. /* Load equivalences. The flag in_load_equiv informs mio_expr_ref of this
  3676. so that unused variables are not loaded and so that the expression can
  3677. be safely freed. */
  3678. static void
  3679. load_equiv (void)
  3680. {
  3681. gfc_equiv *head, *tail, *end, *eq, *equiv;
  3682. bool duplicate;
  3683. mio_lparen ();
  3684. in_load_equiv = true;
  3685. end = gfc_current_ns->equiv;
  3686. while (end != NULL && end->next != NULL)
  3687. end = end->next;
  3688. while (peek_atom () != ATOM_RPAREN) {
  3689. mio_lparen ();
  3690. head = tail = NULL;
  3691. while(peek_atom () != ATOM_RPAREN)
  3692. {
  3693. if (head == NULL)
  3694. head = tail = gfc_get_equiv ();
  3695. else
  3696. {
  3697. tail->eq = gfc_get_equiv ();
  3698. tail = tail->eq;
  3699. }
  3700. mio_pool_string (&tail->module);
  3701. mio_expr (&tail->expr);
  3702. }
  3703. /* Check for duplicate equivalences being loaded from different modules */
  3704. duplicate = false;
  3705. for (equiv = gfc_current_ns->equiv; equiv; equiv = equiv->next)
  3706. {
  3707. if (equiv->module && head->module
  3708. && strcmp (equiv->module, head->module) == 0)
  3709. {
  3710. duplicate = true;
  3711. break;
  3712. }
  3713. }
  3714. if (duplicate)
  3715. {
  3716. for (eq = head; eq; eq = head)
  3717. {
  3718. head = eq->eq;
  3719. gfc_free_expr (eq->expr);
  3720. free (eq);
  3721. }
  3722. }
  3723. if (end == NULL)
  3724. gfc_current_ns->equiv = head;
  3725. else
  3726. end->next = head;
  3727. if (head != NULL)
  3728. end = head;
  3729. mio_rparen ();
  3730. }
  3731. mio_rparen ();
  3732. in_load_equiv = false;
  3733. }
  3734. /* This function loads OpenMP user defined reductions. */
  3735. static void
  3736. load_omp_udrs (void)
  3737. {
  3738. mio_lparen ();
  3739. while (peek_atom () != ATOM_RPAREN)
  3740. {
  3741. const char *name, *newname;
  3742. char *altname;
  3743. gfc_typespec ts;
  3744. gfc_symtree *st;
  3745. gfc_omp_reduction_op rop = OMP_REDUCTION_USER;
  3746. mio_lparen ();
  3747. mio_pool_string (&name);
  3748. mio_typespec (&ts);
  3749. if (strncmp (name, "operator ", sizeof ("operator ") - 1) == 0)
  3750. {
  3751. const char *p = name + sizeof ("operator ") - 1;
  3752. if (strcmp (p, "+") == 0)
  3753. rop = OMP_REDUCTION_PLUS;
  3754. else if (strcmp (p, "*") == 0)
  3755. rop = OMP_REDUCTION_TIMES;
  3756. else if (strcmp (p, "-") == 0)
  3757. rop = OMP_REDUCTION_MINUS;
  3758. else if (strcmp (p, ".and.") == 0)
  3759. rop = OMP_REDUCTION_AND;
  3760. else if (strcmp (p, ".or.") == 0)
  3761. rop = OMP_REDUCTION_OR;
  3762. else if (strcmp (p, ".eqv.") == 0)
  3763. rop = OMP_REDUCTION_EQV;
  3764. else if (strcmp (p, ".neqv.") == 0)
  3765. rop = OMP_REDUCTION_NEQV;
  3766. }
  3767. altname = NULL;
  3768. if (rop == OMP_REDUCTION_USER && name[0] == '.')
  3769. {
  3770. size_t len = strlen (name + 1);
  3771. altname = XALLOCAVEC (char, len);
  3772. gcc_assert (name[len] == '.');
  3773. memcpy (altname, name + 1, len - 1);
  3774. altname[len - 1] = '\0';
  3775. }
  3776. newname = name;
  3777. if (rop == OMP_REDUCTION_USER)
  3778. newname = find_use_name (altname ? altname : name, !!altname);
  3779. else if (only_flag && find_use_operator ((gfc_intrinsic_op) rop) == NULL)
  3780. newname = NULL;
  3781. if (newname == NULL)
  3782. {
  3783. skip_list (1);
  3784. continue;
  3785. }
  3786. if (altname && newname != altname)
  3787. {
  3788. size_t len = strlen (newname);
  3789. altname = XALLOCAVEC (char, len + 3);
  3790. altname[0] = '.';
  3791. memcpy (altname + 1, newname, len);
  3792. altname[len + 1] = '.';
  3793. altname[len + 2] = '\0';
  3794. name = gfc_get_string (altname);
  3795. }
  3796. st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
  3797. gfc_omp_udr *udr = gfc_omp_udr_find (st, &ts);
  3798. if (udr)
  3799. {
  3800. require_atom (ATOM_INTEGER);
  3801. pointer_info *p = get_integer (atom_int);
  3802. if (strcmp (p->u.rsym.module, udr->omp_out->module))
  3803. {
  3804. gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
  3805. "module %s at %L",
  3806. p->u.rsym.module, &gfc_current_locus);
  3807. gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
  3808. "%s at %L",
  3809. udr->omp_out->module, &udr->where);
  3810. }
  3811. skip_list (1);
  3812. continue;
  3813. }
  3814. udr = gfc_get_omp_udr ();
  3815. udr->name = name;
  3816. udr->rop = rop;
  3817. udr->ts = ts;
  3818. udr->where = gfc_current_locus;
  3819. udr->combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
  3820. udr->combiner_ns->proc_name = gfc_current_ns->proc_name;
  3821. mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns,
  3822. false);
  3823. if (peek_atom () != ATOM_RPAREN)
  3824. {
  3825. udr->initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
  3826. udr->initializer_ns->proc_name = gfc_current_ns->proc_name;
  3827. mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
  3828. udr->initializer_ns, true);
  3829. }
  3830. if (st)
  3831. {
  3832. udr->next = st->n.omp_udr;
  3833. st->n.omp_udr = udr;
  3834. }
  3835. else
  3836. {
  3837. st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
  3838. st->n.omp_udr = udr;
  3839. }
  3840. mio_rparen ();
  3841. }
  3842. mio_rparen ();
  3843. }
  3844. /* Recursive function to traverse the pointer_info tree and load a
  3845. needed symbol. We return nonzero if we load a symbol and stop the
  3846. traversal, because the act of loading can alter the tree. */
  3847. static int
  3848. load_needed (pointer_info *p)
  3849. {
  3850. gfc_namespace *ns;
  3851. pointer_info *q;
  3852. gfc_symbol *sym;
  3853. int rv;
  3854. rv = 0;
  3855. if (p == NULL)
  3856. return rv;
  3857. rv |= load_needed (p->left);
  3858. rv |= load_needed (p->right);
  3859. if (p->type != P_SYMBOL || p->u.rsym.state != NEEDED)
  3860. return rv;
  3861. p->u.rsym.state = USED;
  3862. set_module_locus (&p->u.rsym.where);
  3863. sym = p->u.rsym.sym;
  3864. if (sym == NULL)
  3865. {
  3866. q = get_integer (p->u.rsym.ns);
  3867. ns = (gfc_namespace *) q->u.pointer;
  3868. if (ns == NULL)
  3869. {
  3870. /* Create an interface namespace if necessary. These are
  3871. the namespaces that hold the formal parameters of module
  3872. procedures. */
  3873. ns = gfc_get_namespace (NULL, 0);
  3874. associate_integer_pointer (q, ns);
  3875. }
  3876. /* Use the module sym as 'proc_name' so that gfc_get_symbol_decl
  3877. doesn't go pear-shaped if the symbol is used. */
  3878. if (!ns->proc_name)
  3879. gfc_find_symbol (p->u.rsym.module, gfc_current_ns,
  3880. 1, &ns->proc_name);
  3881. sym = gfc_new_symbol (p->u.rsym.true_name, ns);
  3882. sym->name = dt_lower_string (p->u.rsym.true_name);
  3883. sym->module = gfc_get_string (p->u.rsym.module);
  3884. if (p->u.rsym.binding_label)
  3885. sym->binding_label = IDENTIFIER_POINTER (get_identifier
  3886. (p->u.rsym.binding_label));
  3887. associate_integer_pointer (p, sym);
  3888. }
  3889. mio_symbol (sym);
  3890. sym->attr.use_assoc = 1;
  3891. /* Mark as only or rename for later diagnosis for explicitly imported
  3892. but not used warnings; don't mark internal symbols such as __vtab,
  3893. __def_init etc. Only mark them if they have been explicitly loaded. */
  3894. if (only_flag && sym->name[0] != '_' && sym->name[1] != '_')
  3895. {
  3896. gfc_use_rename *u;
  3897. /* Search the use/rename list for the variable; if the variable is
  3898. found, mark it. */
  3899. for (u = gfc_rename_list; u; u = u->next)
  3900. {
  3901. if (strcmp (u->use_name, sym->name) == 0)
  3902. {
  3903. sym->attr.use_only = 1;
  3904. break;
  3905. }
  3906. }
  3907. }
  3908. if (p->u.rsym.renamed)
  3909. sym->attr.use_rename = 1;
  3910. return 1;
  3911. }
  3912. /* Recursive function for cleaning up things after a module has been read. */
  3913. static void
  3914. read_cleanup (pointer_info *p)
  3915. {
  3916. gfc_symtree *st;
  3917. pointer_info *q;
  3918. if (p == NULL)
  3919. return;
  3920. read_cleanup (p->left);
  3921. read_cleanup (p->right);
  3922. if (p->type == P_SYMBOL && p->u.rsym.state == USED && !p->u.rsym.referenced)
  3923. {
  3924. gfc_namespace *ns;
  3925. /* Add hidden symbols to the symtree. */
  3926. q = get_integer (p->u.rsym.ns);
  3927. ns = (gfc_namespace *) q->u.pointer;
  3928. if (!p->u.rsym.sym->attr.vtype
  3929. && !p->u.rsym.sym->attr.vtab)
  3930. st = gfc_get_unique_symtree (ns);
  3931. else
  3932. {
  3933. /* There is no reason to use 'unique_symtrees' for vtabs or
  3934. vtypes - their name is fine for a symtree and reduces the
  3935. namespace pollution. */
  3936. st = gfc_find_symtree (ns->sym_root, p->u.rsym.sym->name);
  3937. if (!st)
  3938. st = gfc_new_symtree (&ns->sym_root, p->u.rsym.sym->name);
  3939. }
  3940. st->n.sym = p->u.rsym.sym;
  3941. st->n.sym->refs++;
  3942. /* Fixup any symtree references. */
  3943. p->u.rsym.symtree = st;
  3944. resolve_fixups (p->u.rsym.stfixup, st);
  3945. p->u.rsym.stfixup = NULL;
  3946. }
  3947. /* Free unused symbols. */
  3948. if (p->type == P_SYMBOL && p->u.rsym.state == UNUSED)
  3949. gfc_free_symbol (p->u.rsym.sym);
  3950. }
  3951. /* It is not quite enough to check for ambiguity in the symbols by
  3952. the loaded symbol and the new symbol not being identical. */
  3953. static bool
  3954. check_for_ambiguous (gfc_symtree *st, pointer_info *info)
  3955. {
  3956. gfc_symbol *rsym;
  3957. module_locus locus;
  3958. symbol_attribute attr;
  3959. gfc_symbol *st_sym;
  3960. if (gfc_current_ns->proc_name && st->name == gfc_current_ns->proc_name->name)
  3961. {
  3962. gfc_error ("%qs of module %qs, imported at %C, is also the name of the "
  3963. "current program unit", st->name, module_name);
  3964. return true;
  3965. }
  3966. st_sym = st->n.sym;
  3967. rsym = info->u.rsym.sym;
  3968. if (st_sym == rsym)
  3969. return false;
  3970. if (st_sym->attr.vtab || st_sym->attr.vtype)
  3971. return false;
  3972. /* If the existing symbol is generic from a different module and
  3973. the new symbol is generic there can be no ambiguity. */
  3974. if (st_sym->attr.generic
  3975. && st_sym->module
  3976. && st_sym->module != module_name)
  3977. {
  3978. /* The new symbol's attributes have not yet been read. Since
  3979. we need attr.generic, read it directly. */
  3980. get_module_locus (&locus);
  3981. set_module_locus (&info->u.rsym.where);
  3982. mio_lparen ();
  3983. attr.generic = 0;
  3984. mio_symbol_attribute (&attr);
  3985. set_module_locus (&locus);
  3986. if (attr.generic)
  3987. return false;
  3988. }
  3989. return true;
  3990. }
  3991. /* Read a module file. */
  3992. static void
  3993. read_module (void)
  3994. {
  3995. module_locus operator_interfaces, user_operators, omp_udrs;
  3996. const char *p;
  3997. char name[GFC_MAX_SYMBOL_LEN + 1];
  3998. int i;
  3999. /* Workaround -Wmaybe-uninitialized false positive during
  4000. profiledbootstrap by initializing them. */
  4001. int ambiguous = 0, j, nuse, symbol = 0;
  4002. pointer_info *info, *q;
  4003. gfc_use_rename *u = NULL;
  4004. gfc_symtree *st;
  4005. gfc_symbol *sym;
  4006. get_module_locus (&operator_interfaces); /* Skip these for now. */
  4007. skip_list ();
  4008. get_module_locus (&user_operators);
  4009. skip_list ();
  4010. skip_list ();
  4011. /* Skip commons and equivalences for now. */
  4012. skip_list ();
  4013. skip_list ();
  4014. /* Skip OpenMP UDRs. */
  4015. get_module_locus (&omp_udrs);
  4016. skip_list ();
  4017. mio_lparen ();
  4018. /* Create the fixup nodes for all the symbols. */
  4019. while (peek_atom () != ATOM_RPAREN)
  4020. {
  4021. char* bind_label;
  4022. require_atom (ATOM_INTEGER);
  4023. info = get_integer (atom_int);
  4024. info->type = P_SYMBOL;
  4025. info->u.rsym.state = UNUSED;
  4026. info->u.rsym.true_name = read_string ();
  4027. info->u.rsym.module = read_string ();
  4028. bind_label = read_string ();
  4029. if (strlen (bind_label))
  4030. info->u.rsym.binding_label = bind_label;
  4031. else
  4032. XDELETEVEC (bind_label);
  4033. require_atom (ATOM_INTEGER);
  4034. info->u.rsym.ns = atom_int;
  4035. get_module_locus (&info->u.rsym.where);
  4036. /* See if the symbol has already been loaded by a previous module.
  4037. If so, we reference the existing symbol and prevent it from
  4038. being loaded again. This should not happen if the symbol being
  4039. read is an index for an assumed shape dummy array (ns != 1). */
  4040. sym = find_true_name (info->u.rsym.true_name, info->u.rsym.module);
  4041. if (sym == NULL
  4042. || (sym->attr.flavor == FL_VARIABLE && info->u.rsym.ns !=1))
  4043. {
  4044. skip_list ();
  4045. continue;
  4046. }
  4047. info->u.rsym.state = USED;
  4048. info->u.rsym.sym = sym;
  4049. /* The current symbol has already been loaded, so we can avoid loading
  4050. it again. However, if it is a derived type, some of its components
  4051. can be used in expressions in the module. To avoid the module loading
  4052. failing, we need to associate the module's component pointer indexes
  4053. with the existing symbol's component pointers. */
  4054. if (sym->attr.flavor == FL_DERIVED)
  4055. {
  4056. gfc_component *c;
  4057. /* First seek to the symbol's component list. */
  4058. mio_lparen (); /* symbol opening. */
  4059. skip_list (); /* skip symbol attribute. */
  4060. mio_lparen (); /* component list opening. */
  4061. for (c = sym->components; c; c = c->next)
  4062. {
  4063. pointer_info *p;
  4064. const char *comp_name;
  4065. int n;
  4066. mio_lparen (); /* component opening. */
  4067. mio_integer (&n);
  4068. p = get_integer (n);
  4069. if (p->u.pointer == NULL)
  4070. associate_integer_pointer (p, c);
  4071. mio_pool_string (&comp_name);
  4072. gcc_assert (comp_name == c->name);
  4073. skip_list (1); /* component end. */
  4074. }
  4075. mio_rparen (); /* component list closing. */
  4076. skip_list (1); /* symbol end. */
  4077. }
  4078. else
  4079. skip_list ();
  4080. /* Some symbols do not have a namespace (eg. formal arguments),
  4081. so the automatic "unique symtree" mechanism must be suppressed
  4082. by marking them as referenced. */
  4083. q = get_integer (info->u.rsym.ns);
  4084. if (q->u.pointer == NULL)
  4085. {
  4086. info->u.rsym.referenced = 1;
  4087. continue;
  4088. }
  4089. /* If possible recycle the symtree that references the symbol.
  4090. If a symtree is not found and the module does not import one,
  4091. a unique-name symtree is found by read_cleanup. */
  4092. st = find_symtree_for_symbol (gfc_current_ns->sym_root, sym);
  4093. if (st != NULL)
  4094. {
  4095. info->u.rsym.symtree = st;
  4096. info->u.rsym.referenced = 1;
  4097. }
  4098. }
  4099. mio_rparen ();
  4100. /* Parse the symtree lists. This lets us mark which symbols need to
  4101. be loaded. Renaming is also done at this point by replacing the
  4102. symtree name. */
  4103. mio_lparen ();
  4104. while (peek_atom () != ATOM_RPAREN)
  4105. {
  4106. mio_internal_string (name);
  4107. mio_integer (&ambiguous);
  4108. mio_integer (&symbol);
  4109. info = get_integer (symbol);
  4110. /* See how many use names there are. If none, go through the start
  4111. of the loop at least once. */
  4112. nuse = number_use_names (name, false);
  4113. info->u.rsym.renamed = nuse ? 1 : 0;
  4114. if (nuse == 0)
  4115. nuse = 1;
  4116. for (j = 1; j <= nuse; j++)
  4117. {
  4118. /* Get the jth local name for this symbol. */
  4119. p = find_use_name_n (name, &j, false);
  4120. if (p == NULL && strcmp (name, module_name) == 0)
  4121. p = name;
  4122. /* Exception: Always import vtabs & vtypes. */
  4123. if (p == NULL && name[0] == '_'
  4124. && (strncmp (name, "__vtab_", 5) == 0
  4125. || strncmp (name, "__vtype_", 6) == 0))
  4126. p = name;
  4127. /* Skip symtree nodes not in an ONLY clause, unless there
  4128. is an existing symtree loaded from another USE statement. */
  4129. if (p == NULL)
  4130. {
  4131. st = gfc_find_symtree (gfc_current_ns->sym_root, name);
  4132. if (st != NULL
  4133. && strcmp (st->n.sym->name, info->u.rsym.true_name) == 0
  4134. && st->n.sym->module != NULL
  4135. && strcmp (st->n.sym->module, info->u.rsym.module) == 0)
  4136. {
  4137. info->u.rsym.symtree = st;
  4138. info->u.rsym.sym = st->n.sym;
  4139. }
  4140. continue;
  4141. }
  4142. /* If a symbol of the same name and module exists already,
  4143. this symbol, which is not in an ONLY clause, must not be
  4144. added to the namespace(11.3.2). Note that find_symbol
  4145. only returns the first occurrence that it finds. */
  4146. if (!only_flag && !info->u.rsym.renamed
  4147. && strcmp (name, module_name) != 0
  4148. && find_symbol (gfc_current_ns->sym_root, name,
  4149. module_name, 0))
  4150. continue;
  4151. st = gfc_find_symtree (gfc_current_ns->sym_root, p);
  4152. if (st != NULL)
  4153. {
  4154. /* Check for ambiguous symbols. */
  4155. if (check_for_ambiguous (st, info))
  4156. st->ambiguous = 1;
  4157. else
  4158. info->u.rsym.symtree = st;
  4159. }
  4160. else
  4161. {
  4162. st = gfc_find_symtree (gfc_current_ns->sym_root, name);
  4163. /* Create a symtree node in the current namespace for this
  4164. symbol. */
  4165. st = check_unique_name (p)
  4166. ? gfc_get_unique_symtree (gfc_current_ns)
  4167. : gfc_new_symtree (&gfc_current_ns->sym_root, p);
  4168. st->ambiguous = ambiguous;
  4169. sym = info->u.rsym.sym;
  4170. /* Create a symbol node if it doesn't already exist. */
  4171. if (sym == NULL)
  4172. {
  4173. info->u.rsym.sym = gfc_new_symbol (info->u.rsym.true_name,
  4174. gfc_current_ns);
  4175. info->u.rsym.sym->name = dt_lower_string (info->u.rsym.true_name);
  4176. sym = info->u.rsym.sym;
  4177. sym->module = gfc_get_string (info->u.rsym.module);
  4178. if (info->u.rsym.binding_label)
  4179. sym->binding_label =
  4180. IDENTIFIER_POINTER (get_identifier
  4181. (info->u.rsym.binding_label));
  4182. }
  4183. st->n.sym = sym;
  4184. st->n.sym->refs++;
  4185. if (strcmp (name, p) != 0)
  4186. sym->attr.use_rename = 1;
  4187. if (name[0] != '_'
  4188. || (strncmp (name, "__vtab_", 5) != 0
  4189. && strncmp (name, "__vtype_", 6) != 0))
  4190. sym->attr.use_only = only_flag;
  4191. /* Store the symtree pointing to this symbol. */
  4192. info->u.rsym.symtree = st;
  4193. if (info->u.rsym.state == UNUSED)
  4194. info->u.rsym.state = NEEDED;
  4195. info->u.rsym.referenced = 1;
  4196. }
  4197. }
  4198. }
  4199. mio_rparen ();
  4200. /* Load intrinsic operator interfaces. */
  4201. set_module_locus (&operator_interfaces);
  4202. mio_lparen ();
  4203. for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
  4204. {
  4205. if (i == INTRINSIC_USER)
  4206. continue;
  4207. if (only_flag)
  4208. {
  4209. u = find_use_operator ((gfc_intrinsic_op) i);
  4210. if (u == NULL)
  4211. {
  4212. skip_list ();
  4213. continue;
  4214. }
  4215. u->found = 1;
  4216. }
  4217. mio_interface (&gfc_current_ns->op[i]);
  4218. if (u && !gfc_current_ns->op[i])
  4219. u->found = 0;
  4220. }
  4221. mio_rparen ();
  4222. /* Load generic and user operator interfaces. These must follow the
  4223. loading of symtree because otherwise symbols can be marked as
  4224. ambiguous. */
  4225. set_module_locus (&user_operators);
  4226. load_operator_interfaces ();
  4227. load_generic_interfaces ();
  4228. load_commons ();
  4229. load_equiv ();
  4230. /* Load OpenMP user defined reductions. */
  4231. set_module_locus (&omp_udrs);
  4232. load_omp_udrs ();
  4233. /* At this point, we read those symbols that are needed but haven't
  4234. been loaded yet. If one symbol requires another, the other gets
  4235. marked as NEEDED if its previous state was UNUSED. */
  4236. while (load_needed (pi_root));
  4237. /* Make sure all elements of the rename-list were found in the module. */
  4238. for (u = gfc_rename_list; u; u = u->next)
  4239. {
  4240. if (u->found)
  4241. continue;
  4242. if (u->op == INTRINSIC_NONE)
  4243. {
  4244. gfc_error ("Symbol %qs referenced at %L not found in module %qs",
  4245. u->use_name, &u->where, module_name);
  4246. continue;
  4247. }
  4248. if (u->op == INTRINSIC_USER)
  4249. {
  4250. gfc_error ("User operator %qs referenced at %L not found "
  4251. "in module %qs", u->use_name, &u->where, module_name);
  4252. continue;
  4253. }
  4254. gfc_error ("Intrinsic operator %qs referenced at %L not found "
  4255. "in module %qs", gfc_op2string (u->op), &u->where,
  4256. module_name);
  4257. }
  4258. /* Clean up symbol nodes that were never loaded, create references
  4259. to hidden symbols. */
  4260. read_cleanup (pi_root);
  4261. }
  4262. /* Given an access type that is specific to an entity and the default
  4263. access, return nonzero if the entity is publicly accessible. If the
  4264. element is declared as PUBLIC, then it is public; if declared
  4265. PRIVATE, then private, and otherwise it is public unless the default
  4266. access in this context has been declared PRIVATE. */
  4267. static bool
  4268. check_access (gfc_access specific_access, gfc_access default_access)
  4269. {
  4270. if (specific_access == ACCESS_PUBLIC)
  4271. return TRUE;
  4272. if (specific_access == ACCESS_PRIVATE)
  4273. return FALSE;
  4274. if (flag_module_private)
  4275. return default_access == ACCESS_PUBLIC;
  4276. else
  4277. return default_access != ACCESS_PRIVATE;
  4278. }
  4279. bool
  4280. gfc_check_symbol_access (gfc_symbol *sym)
  4281. {
  4282. if (sym->attr.vtab || sym->attr.vtype)
  4283. return true;
  4284. else
  4285. return check_access (sym->attr.access, sym->ns->default_access);
  4286. }
  4287. /* A structure to remember which commons we've already written. */
  4288. struct written_common
  4289. {
  4290. BBT_HEADER(written_common);
  4291. const char *name, *label;
  4292. };
  4293. static struct written_common *written_commons = NULL;
  4294. /* Comparison function used for balancing the binary tree. */
  4295. static int
  4296. compare_written_commons (void *a1, void *b1)
  4297. {
  4298. const char *aname = ((struct written_common *) a1)->name;
  4299. const char *alabel = ((struct written_common *) a1)->label;
  4300. const char *bname = ((struct written_common *) b1)->name;
  4301. const char *blabel = ((struct written_common *) b1)->label;
  4302. int c = strcmp (aname, bname);
  4303. return (c != 0 ? c : strcmp (alabel, blabel));
  4304. }
  4305. /* Free a list of written commons. */
  4306. static void
  4307. free_written_common (struct written_common *w)
  4308. {
  4309. if (!w)
  4310. return;
  4311. if (w->left)
  4312. free_written_common (w->left);
  4313. if (w->right)
  4314. free_written_common (w->right);
  4315. free (w);
  4316. }
  4317. /* Write a common block to the module -- recursive helper function. */
  4318. static void
  4319. write_common_0 (gfc_symtree *st, bool this_module)
  4320. {
  4321. gfc_common_head *p;
  4322. const char * name;
  4323. int flags;
  4324. const char *label;
  4325. struct written_common *w;
  4326. bool write_me = true;
  4327. if (st == NULL)
  4328. return;
  4329. write_common_0 (st->left, this_module);
  4330. /* We will write out the binding label, or "" if no label given. */
  4331. name = st->n.common->name;
  4332. p = st->n.common;
  4333. label = (p->is_bind_c && p->binding_label) ? p->binding_label : "";
  4334. /* Check if we've already output this common. */
  4335. w = written_commons;
  4336. while (w)
  4337. {
  4338. int c = strcmp (name, w->name);
  4339. c = (c != 0 ? c : strcmp (label, w->label));
  4340. if (c == 0)
  4341. write_me = false;
  4342. w = (c < 0) ? w->left : w->right;
  4343. }
  4344. if (this_module && p->use_assoc)
  4345. write_me = false;
  4346. if (write_me)
  4347. {
  4348. /* Write the common to the module. */
  4349. mio_lparen ();
  4350. mio_pool_string (&name);
  4351. mio_symbol_ref (&p->head);
  4352. flags = p->saved ? 1 : 0;
  4353. if (p->threadprivate)
  4354. flags |= 2;
  4355. mio_integer (&flags);
  4356. /* Write out whether the common block is bind(c) or not. */
  4357. mio_integer (&(p->is_bind_c));
  4358. mio_pool_string (&label);
  4359. mio_rparen ();
  4360. /* Record that we have written this common. */
  4361. w = XCNEW (struct written_common);
  4362. w->name = p->name;
  4363. w->label = label;
  4364. gfc_insert_bbt (&written_commons, w, compare_written_commons);
  4365. }
  4366. write_common_0 (st->right, this_module);
  4367. }
  4368. /* Write a common, by initializing the list of written commons, calling
  4369. the recursive function write_common_0() and cleaning up afterwards. */
  4370. static void
  4371. write_common (gfc_symtree *st)
  4372. {
  4373. written_commons = NULL;
  4374. write_common_0 (st, true);
  4375. write_common_0 (st, false);
  4376. free_written_common (written_commons);
  4377. written_commons = NULL;
  4378. }
  4379. /* Write the blank common block to the module. */
  4380. static void
  4381. write_blank_common (void)
  4382. {
  4383. const char * name = BLANK_COMMON_NAME;
  4384. int saved;
  4385. /* TODO: Blank commons are not bind(c). The F2003 standard probably says
  4386. this, but it hasn't been checked. Just making it so for now. */
  4387. int is_bind_c = 0;
  4388. if (gfc_current_ns->blank_common.head == NULL)
  4389. return;
  4390. mio_lparen ();
  4391. mio_pool_string (&name);
  4392. mio_symbol_ref (&gfc_current_ns->blank_common.head);
  4393. saved = gfc_current_ns->blank_common.saved;
  4394. mio_integer (&saved);
  4395. /* Write out whether the common block is bind(c) or not. */
  4396. mio_integer (&is_bind_c);
  4397. /* Write out an empty binding label. */
  4398. write_atom (ATOM_STRING, "");
  4399. mio_rparen ();
  4400. }
  4401. /* Write equivalences to the module. */
  4402. static void
  4403. write_equiv (void)
  4404. {
  4405. gfc_equiv *eq, *e;
  4406. int num;
  4407. num = 0;
  4408. for (eq = gfc_current_ns->equiv; eq; eq = eq->next)
  4409. {
  4410. mio_lparen ();
  4411. for (e = eq; e; e = e->eq)
  4412. {
  4413. if (e->module == NULL)
  4414. e->module = gfc_get_string ("%s.eq.%d", module_name, num);
  4415. mio_allocated_string (e->module);
  4416. mio_expr (&e->expr);
  4417. }
  4418. num++;
  4419. mio_rparen ();
  4420. }
  4421. }
  4422. /* Write a symbol to the module. */
  4423. static void
  4424. write_symbol (int n, gfc_symbol *sym)
  4425. {
  4426. const char *label;
  4427. if (sym->attr.flavor == FL_UNKNOWN || sym->attr.flavor == FL_LABEL)
  4428. gfc_internal_error ("write_symbol(): bad module symbol %qs", sym->name);
  4429. mio_integer (&n);
  4430. if (sym->attr.flavor == FL_DERIVED)
  4431. {
  4432. const char *name;
  4433. name = dt_upper_string (sym->name);
  4434. mio_pool_string (&name);
  4435. }
  4436. else
  4437. mio_pool_string (&sym->name);
  4438. mio_pool_string (&sym->module);
  4439. if ((sym->attr.is_bind_c || sym->attr.is_iso_c) && sym->binding_label)
  4440. {
  4441. label = sym->binding_label;
  4442. mio_pool_string (&label);
  4443. }
  4444. else
  4445. write_atom (ATOM_STRING, "");
  4446. mio_pointer_ref (&sym->ns);
  4447. mio_symbol (sym);
  4448. write_char ('\n');
  4449. }
  4450. /* Recursive traversal function to write the initial set of symbols to
  4451. the module. We check to see if the symbol should be written
  4452. according to the access specification. */
  4453. static void
  4454. write_symbol0 (gfc_symtree *st)
  4455. {
  4456. gfc_symbol *sym;
  4457. pointer_info *p;
  4458. bool dont_write = false;
  4459. if (st == NULL)
  4460. return;
  4461. write_symbol0 (st->left);
  4462. sym = st->n.sym;
  4463. if (sym->module == NULL)
  4464. sym->module = module_name;
  4465. if (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
  4466. && !sym->attr.subroutine && !sym->attr.function)
  4467. dont_write = true;
  4468. if (!gfc_check_symbol_access (sym))
  4469. dont_write = true;
  4470. if (!dont_write)
  4471. {
  4472. p = get_pointer (sym);
  4473. if (p->type == P_UNKNOWN)
  4474. p->type = P_SYMBOL;
  4475. if (p->u.wsym.state != WRITTEN)
  4476. {
  4477. write_symbol (p->integer, sym);
  4478. p->u.wsym.state = WRITTEN;
  4479. }
  4480. }
  4481. write_symbol0 (st->right);
  4482. }
  4483. static void
  4484. write_omp_udr (gfc_omp_udr *udr)
  4485. {
  4486. switch (udr->rop)
  4487. {
  4488. case OMP_REDUCTION_USER:
  4489. /* Non-operators can't be used outside of the module. */
  4490. if (udr->name[0] != '.')
  4491. return;
  4492. else
  4493. {
  4494. gfc_symtree *st;
  4495. size_t len = strlen (udr->name + 1);
  4496. char *name = XALLOCAVEC (char, len);
  4497. memcpy (name, udr->name, len - 1);
  4498. name[len - 1] = '\0';
  4499. st = gfc_find_symtree (gfc_current_ns->uop_root, name);
  4500. /* If corresponding user operator is private, don't write
  4501. the UDR. */
  4502. if (st != NULL)
  4503. {
  4504. gfc_user_op *uop = st->n.uop;
  4505. if (!check_access (uop->access, uop->ns->default_access))
  4506. return;
  4507. }
  4508. }
  4509. break;
  4510. case OMP_REDUCTION_PLUS:
  4511. case OMP_REDUCTION_MINUS:
  4512. case OMP_REDUCTION_TIMES:
  4513. case OMP_REDUCTION_AND:
  4514. case OMP_REDUCTION_OR:
  4515. case OMP_REDUCTION_EQV:
  4516. case OMP_REDUCTION_NEQV:
  4517. /* If corresponding operator is private, don't write the UDR. */
  4518. if (!check_access (gfc_current_ns->operator_access[udr->rop],
  4519. gfc_current_ns->default_access))
  4520. return;
  4521. break;
  4522. default:
  4523. break;
  4524. }
  4525. if (udr->ts.type == BT_DERIVED || udr->ts.type == BT_CLASS)
  4526. {
  4527. /* If derived type is private, don't write the UDR. */
  4528. if (!gfc_check_symbol_access (udr->ts.u.derived))
  4529. return;
  4530. }
  4531. mio_lparen ();
  4532. mio_pool_string (&udr->name);
  4533. mio_typespec (&udr->ts);
  4534. mio_omp_udr_expr (udr, &udr->omp_out, &udr->omp_in, udr->combiner_ns, false);
  4535. if (udr->initializer_ns)
  4536. mio_omp_udr_expr (udr, &udr->omp_priv, &udr->omp_orig,
  4537. udr->initializer_ns, true);
  4538. mio_rparen ();
  4539. }
  4540. static void
  4541. write_omp_udrs (gfc_symtree *st)
  4542. {
  4543. if (st == NULL)
  4544. return;
  4545. write_omp_udrs (st->left);
  4546. gfc_omp_udr *udr;
  4547. for (udr = st->n.omp_udr; udr; udr = udr->next)
  4548. write_omp_udr (udr);
  4549. write_omp_udrs (st->right);
  4550. }
  4551. /* Type for the temporary tree used when writing secondary symbols. */
  4552. struct sorted_pointer_info
  4553. {
  4554. BBT_HEADER (sorted_pointer_info);
  4555. pointer_info *p;
  4556. };
  4557. #define gfc_get_sorted_pointer_info() XCNEW (sorted_pointer_info)
  4558. /* Recursively traverse the temporary tree, free its contents. */
  4559. static void
  4560. free_sorted_pointer_info_tree (sorted_pointer_info *p)
  4561. {
  4562. if (!p)
  4563. return;
  4564. free_sorted_pointer_info_tree (p->left);
  4565. free_sorted_pointer_info_tree (p->right);
  4566. free (p);
  4567. }
  4568. /* Comparison function for the temporary tree. */
  4569. static int
  4570. compare_sorted_pointer_info (void *_spi1, void *_spi2)
  4571. {
  4572. sorted_pointer_info *spi1, *spi2;
  4573. spi1 = (sorted_pointer_info *)_spi1;
  4574. spi2 = (sorted_pointer_info *)_spi2;
  4575. if (spi1->p->integer < spi2->p->integer)
  4576. return -1;
  4577. if (spi1->p->integer > spi2->p->integer)
  4578. return 1;
  4579. return 0;
  4580. }
  4581. /* Finds the symbols that need to be written and collects them in the
  4582. sorted_pi tree so that they can be traversed in an order
  4583. independent of memory addresses. */
  4584. static void
  4585. find_symbols_to_write(sorted_pointer_info **tree, pointer_info *p)
  4586. {
  4587. if (!p)
  4588. return;
  4589. if (p->type == P_SYMBOL && p->u.wsym.state == NEEDS_WRITE)
  4590. {
  4591. sorted_pointer_info *sp = gfc_get_sorted_pointer_info();
  4592. sp->p = p;
  4593. gfc_insert_bbt (tree, sp, compare_sorted_pointer_info);
  4594. }
  4595. find_symbols_to_write (tree, p->left);
  4596. find_symbols_to_write (tree, p->right);
  4597. }
  4598. /* Recursive function that traverses the tree of symbols that need to be
  4599. written and writes them in order. */
  4600. static void
  4601. write_symbol1_recursion (sorted_pointer_info *sp)
  4602. {
  4603. if (!sp)
  4604. return;
  4605. write_symbol1_recursion (sp->left);
  4606. pointer_info *p1 = sp->p;
  4607. gcc_assert (p1->type == P_SYMBOL && p1->u.wsym.state == NEEDS_WRITE);
  4608. p1->u.wsym.state = WRITTEN;
  4609. write_symbol (p1->integer, p1->u.wsym.sym);
  4610. p1->u.wsym.sym->attr.public_used = 1;
  4611. write_symbol1_recursion (sp->right);
  4612. }
  4613. /* Write the secondary set of symbols to the module file. These are
  4614. symbols that were not public yet are needed by the public symbols
  4615. or another dependent symbol. The act of writing a symbol can add
  4616. symbols to the pointer_info tree, so we return nonzero if a symbol
  4617. was written and pass that information upwards. The caller will
  4618. then call this function again until nothing was written. It uses
  4619. the utility functions and a temporary tree to ensure a reproducible
  4620. ordering of the symbol output and thus the module file. */
  4621. static int
  4622. write_symbol1 (pointer_info *p)
  4623. {
  4624. if (!p)
  4625. return 0;
  4626. /* Put symbols that need to be written into a tree sorted on the
  4627. integer field. */
  4628. sorted_pointer_info *spi_root = NULL;
  4629. find_symbols_to_write (&spi_root, p);
  4630. /* No symbols to write, return. */
  4631. if (!spi_root)
  4632. return 0;
  4633. /* Otherwise, write and free the tree again. */
  4634. write_symbol1_recursion (spi_root);
  4635. free_sorted_pointer_info_tree (spi_root);
  4636. return 1;
  4637. }
  4638. /* Write operator interfaces associated with a symbol. */
  4639. static void
  4640. write_operator (gfc_user_op *uop)
  4641. {
  4642. static char nullstring[] = "";
  4643. const char *p = nullstring;
  4644. if (uop->op == NULL || !check_access (uop->access, uop->ns->default_access))
  4645. return;
  4646. mio_symbol_interface (&uop->name, &p, &uop->op);
  4647. }
  4648. /* Write generic interfaces from the namespace sym_root. */
  4649. static void
  4650. write_generic (gfc_symtree *st)
  4651. {
  4652. gfc_symbol *sym;
  4653. if (st == NULL)
  4654. return;
  4655. write_generic (st->left);
  4656. sym = st->n.sym;
  4657. if (sym && !check_unique_name (st->name)
  4658. && sym->generic && gfc_check_symbol_access (sym))
  4659. {
  4660. if (!sym->module)
  4661. sym->module = module_name;
  4662. mio_symbol_interface (&st->name, &sym->module, &sym->generic);
  4663. }
  4664. write_generic (st->right);
  4665. }
  4666. static void
  4667. write_symtree (gfc_symtree *st)
  4668. {
  4669. gfc_symbol *sym;
  4670. pointer_info *p;
  4671. sym = st->n.sym;
  4672. /* A symbol in an interface body must not be visible in the
  4673. module file. */
  4674. if (sym->ns != gfc_current_ns
  4675. && sym->ns->proc_name
  4676. && sym->ns->proc_name->attr.if_source == IFSRC_IFBODY)
  4677. return;
  4678. if (!gfc_check_symbol_access (sym)
  4679. || (sym->attr.flavor == FL_PROCEDURE && sym->attr.generic
  4680. && !sym->attr.subroutine && !sym->attr.function))
  4681. return;
  4682. if (check_unique_name (st->name))
  4683. return;
  4684. p = find_pointer (sym);
  4685. if (p == NULL)
  4686. gfc_internal_error ("write_symtree(): Symbol not written");
  4687. mio_pool_string (&st->name);
  4688. mio_integer (&st->ambiguous);
  4689. mio_integer (&p->integer);
  4690. }
  4691. static void
  4692. write_module (void)
  4693. {
  4694. int i;
  4695. /* Write the operator interfaces. */
  4696. mio_lparen ();
  4697. for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
  4698. {
  4699. if (i == INTRINSIC_USER)
  4700. continue;
  4701. mio_interface (check_access (gfc_current_ns->operator_access[i],
  4702. gfc_current_ns->default_access)
  4703. ? &gfc_current_ns->op[i] : NULL);
  4704. }
  4705. mio_rparen ();
  4706. write_char ('\n');
  4707. write_char ('\n');
  4708. mio_lparen ();
  4709. gfc_traverse_user_op (gfc_current_ns, write_operator);
  4710. mio_rparen ();
  4711. write_char ('\n');
  4712. write_char ('\n');
  4713. mio_lparen ();
  4714. write_generic (gfc_current_ns->sym_root);
  4715. mio_rparen ();
  4716. write_char ('\n');
  4717. write_char ('\n');
  4718. mio_lparen ();
  4719. write_blank_common ();
  4720. write_common (gfc_current_ns->common_root);
  4721. mio_rparen ();
  4722. write_char ('\n');
  4723. write_char ('\n');
  4724. mio_lparen ();
  4725. write_equiv ();
  4726. mio_rparen ();
  4727. write_char ('\n');
  4728. write_char ('\n');
  4729. mio_lparen ();
  4730. write_omp_udrs (gfc_current_ns->omp_udr_root);
  4731. mio_rparen ();
  4732. write_char ('\n');
  4733. write_char ('\n');
  4734. /* Write symbol information. First we traverse all symbols in the
  4735. primary namespace, writing those that need to be written.
  4736. Sometimes writing one symbol will cause another to need to be
  4737. written. A list of these symbols ends up on the write stack, and
  4738. we end by popping the bottom of the stack and writing the symbol
  4739. until the stack is empty. */
  4740. mio_lparen ();
  4741. write_symbol0 (gfc_current_ns->sym_root);
  4742. while (write_symbol1 (pi_root))
  4743. /* Nothing. */;
  4744. mio_rparen ();
  4745. write_char ('\n');
  4746. write_char ('\n');
  4747. mio_lparen ();
  4748. gfc_traverse_symtree (gfc_current_ns->sym_root, write_symtree);
  4749. mio_rparen ();
  4750. }
  4751. /* Read a CRC32 sum from the gzip trailer of a module file. Returns
  4752. true on success, false on failure. */
  4753. static bool
  4754. read_crc32_from_module_file (const char* filename, uLong* crc)
  4755. {
  4756. FILE *file;
  4757. char buf[4];
  4758. unsigned int val;
  4759. /* Open the file in binary mode. */
  4760. if ((file = fopen (filename, "rb")) == NULL)
  4761. return false;
  4762. /* The gzip crc32 value is found in the [END-8, END-4] bytes of the
  4763. file. See RFC 1952. */
  4764. if (fseek (file, -8, SEEK_END) != 0)
  4765. {
  4766. fclose (file);
  4767. return false;
  4768. }
  4769. /* Read the CRC32. */
  4770. if (fread (buf, 1, 4, file) != 4)
  4771. {
  4772. fclose (file);
  4773. return false;
  4774. }
  4775. /* Close the file. */
  4776. fclose (file);
  4777. val = (buf[0] & 0xFF) + ((buf[1] & 0xFF) << 8) + ((buf[2] & 0xFF) << 16)
  4778. + ((buf[3] & 0xFF) << 24);
  4779. *crc = val;
  4780. /* For debugging, the CRC value printed in hexadecimal should match
  4781. the CRC printed by "zcat -l -v filename".
  4782. printf("CRC of file %s is %x\n", filename, val); */
  4783. return true;
  4784. }
  4785. /* Given module, dump it to disk. If there was an error while
  4786. processing the module, dump_flag will be set to zero and we delete
  4787. the module file, even if it was already there. */
  4788. void
  4789. gfc_dump_module (const char *name, int dump_flag)
  4790. {
  4791. int n;
  4792. char *filename, *filename_tmp;
  4793. uLong crc, crc_old;
  4794. n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
  4795. if (gfc_option.module_dir != NULL)
  4796. {
  4797. n += strlen (gfc_option.module_dir);
  4798. filename = (char *) alloca (n);
  4799. strcpy (filename, gfc_option.module_dir);
  4800. strcat (filename, name);
  4801. }
  4802. else
  4803. {
  4804. filename = (char *) alloca (n);
  4805. strcpy (filename, name);
  4806. }
  4807. strcat (filename, MODULE_EXTENSION);
  4808. /* Name of the temporary file used to write the module. */
  4809. filename_tmp = (char *) alloca (n + 1);
  4810. strcpy (filename_tmp, filename);
  4811. strcat (filename_tmp, "0");
  4812. /* There was an error while processing the module. We delete the
  4813. module file, even if it was already there. */
  4814. if (!dump_flag)
  4815. {
  4816. remove (filename);
  4817. return;
  4818. }
  4819. if (gfc_cpp_makedep ())
  4820. gfc_cpp_add_target (filename);
  4821. /* Write the module to the temporary file. */
  4822. module_fp = gzopen (filename_tmp, "w");
  4823. if (module_fp == NULL)
  4824. gfc_fatal_error ("Can't open module file %qs for writing at %C: %s",
  4825. filename_tmp, xstrerror (errno));
  4826. gzprintf (module_fp, "GFORTRAN module version '%s' created from %s\n",
  4827. MOD_VERSION, gfc_source_file);
  4828. /* Write the module itself. */
  4829. iomode = IO_OUTPUT;
  4830. module_name = gfc_get_string (name);
  4831. init_pi_tree ();
  4832. write_module ();
  4833. free_pi_tree (pi_root);
  4834. pi_root = NULL;
  4835. write_char ('\n');
  4836. if (gzclose (module_fp))
  4837. gfc_fatal_error ("Error writing module file %qs for writing: %s",
  4838. filename_tmp, xstrerror (errno));
  4839. /* Read the CRC32 from the gzip trailers of the module files and
  4840. compare. */
  4841. if (!read_crc32_from_module_file (filename_tmp, &crc)
  4842. || !read_crc32_from_module_file (filename, &crc_old)
  4843. || crc_old != crc)
  4844. {
  4845. /* Module file have changed, replace the old one. */
  4846. if (remove (filename) && errno != ENOENT)
  4847. gfc_fatal_error ("Can't delete module file %qs: %s", filename,
  4848. xstrerror (errno));
  4849. if (rename (filename_tmp, filename))
  4850. gfc_fatal_error ("Can't rename module file %qs to %qs: %s",
  4851. filename_tmp, filename, xstrerror (errno));
  4852. }
  4853. else
  4854. {
  4855. if (remove (filename_tmp))
  4856. gfc_fatal_error ("Can't delete temporary module file %qs: %s",
  4857. filename_tmp, xstrerror (errno));
  4858. }
  4859. }
  4860. static void
  4861. create_intrinsic_function (const char *name, int id,
  4862. const char *modname, intmod_id module,
  4863. bool subroutine, gfc_symbol *result_type)
  4864. {
  4865. gfc_intrinsic_sym *isym;
  4866. gfc_symtree *tmp_symtree;
  4867. gfc_symbol *sym;
  4868. tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
  4869. if (tmp_symtree)
  4870. {
  4871. if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
  4872. return;
  4873. gfc_error ("Symbol %qs already declared", name);
  4874. }
  4875. gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
  4876. sym = tmp_symtree->n.sym;
  4877. if (subroutine)
  4878. {
  4879. gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
  4880. isym = gfc_intrinsic_subroutine_by_id (isym_id);
  4881. sym->attr.subroutine = 1;
  4882. }
  4883. else
  4884. {
  4885. gfc_isym_id isym_id = gfc_isym_id_by_intmod (module, id);
  4886. isym = gfc_intrinsic_function_by_id (isym_id);
  4887. sym->attr.function = 1;
  4888. if (result_type)
  4889. {
  4890. sym->ts.type = BT_DERIVED;
  4891. sym->ts.u.derived = result_type;
  4892. sym->ts.is_c_interop = 1;
  4893. isym->ts.f90_type = BT_VOID;
  4894. isym->ts.type = BT_DERIVED;
  4895. isym->ts.f90_type = BT_VOID;
  4896. isym->ts.u.derived = result_type;
  4897. isym->ts.is_c_interop = 1;
  4898. }
  4899. }
  4900. gcc_assert (isym);
  4901. sym->attr.flavor = FL_PROCEDURE;
  4902. sym->attr.intrinsic = 1;
  4903. sym->module = gfc_get_string (modname);
  4904. sym->attr.use_assoc = 1;
  4905. sym->from_intmod = module;
  4906. sym->intmod_sym_id = id;
  4907. }
  4908. /* Import the intrinsic ISO_C_BINDING module, generating symbols in
  4909. the current namespace for all named constants, pointer types, and
  4910. procedures in the module unless the only clause was used or a rename
  4911. list was provided. */
  4912. static void
  4913. import_iso_c_binding_module (void)
  4914. {
  4915. gfc_symbol *mod_sym = NULL, *return_type;
  4916. gfc_symtree *mod_symtree = NULL, *tmp_symtree;
  4917. gfc_symtree *c_ptr = NULL, *c_funptr = NULL;
  4918. const char *iso_c_module_name = "__iso_c_binding";
  4919. gfc_use_rename *u;
  4920. int i;
  4921. bool want_c_ptr = false, want_c_funptr = false;
  4922. /* Look only in the current namespace. */
  4923. mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, iso_c_module_name);
  4924. if (mod_symtree == NULL)
  4925. {
  4926. /* symtree doesn't already exist in current namespace. */
  4927. gfc_get_sym_tree (iso_c_module_name, gfc_current_ns, &mod_symtree,
  4928. false);
  4929. if (mod_symtree != NULL)
  4930. mod_sym = mod_symtree->n.sym;
  4931. else
  4932. gfc_internal_error ("import_iso_c_binding_module(): Unable to "
  4933. "create symbol for %s", iso_c_module_name);
  4934. mod_sym->attr.flavor = FL_MODULE;
  4935. mod_sym->attr.intrinsic = 1;
  4936. mod_sym->module = gfc_get_string (iso_c_module_name);
  4937. mod_sym->from_intmod = INTMOD_ISO_C_BINDING;
  4938. }
  4939. /* Check whether C_PTR or C_FUNPTR are in the include list, if so, load it;
  4940. check also whether C_NULL_(FUN)PTR or C_(FUN)LOC are requested, which
  4941. need C_(FUN)PTR. */
  4942. for (u = gfc_rename_list; u; u = u->next)
  4943. {
  4944. if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_PTR].name,
  4945. u->use_name) == 0)
  4946. want_c_ptr = true;
  4947. else if (strcmp (c_interop_kinds_table[ISOCBINDING_LOC].name,
  4948. u->use_name) == 0)
  4949. want_c_ptr = true;
  4950. else if (strcmp (c_interop_kinds_table[ISOCBINDING_NULL_FUNPTR].name,
  4951. u->use_name) == 0)
  4952. want_c_funptr = true;
  4953. else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNLOC].name,
  4954. u->use_name) == 0)
  4955. want_c_funptr = true;
  4956. else if (strcmp (c_interop_kinds_table[ISOCBINDING_PTR].name,
  4957. u->use_name) == 0)
  4958. {
  4959. c_ptr = generate_isocbinding_symbol (iso_c_module_name,
  4960. (iso_c_binding_symbol)
  4961. ISOCBINDING_PTR,
  4962. u->local_name[0] ? u->local_name
  4963. : u->use_name,
  4964. NULL, false);
  4965. }
  4966. else if (strcmp (c_interop_kinds_table[ISOCBINDING_FUNPTR].name,
  4967. u->use_name) == 0)
  4968. {
  4969. c_funptr
  4970. = generate_isocbinding_symbol (iso_c_module_name,
  4971. (iso_c_binding_symbol)
  4972. ISOCBINDING_FUNPTR,
  4973. u->local_name[0] ? u->local_name
  4974. : u->use_name,
  4975. NULL, false);
  4976. }
  4977. }
  4978. if ((want_c_ptr || !only_flag) && !c_ptr)
  4979. c_ptr = generate_isocbinding_symbol (iso_c_module_name,
  4980. (iso_c_binding_symbol)
  4981. ISOCBINDING_PTR,
  4982. NULL, NULL, only_flag);
  4983. if ((want_c_funptr || !only_flag) && !c_funptr)
  4984. c_funptr = generate_isocbinding_symbol (iso_c_module_name,
  4985. (iso_c_binding_symbol)
  4986. ISOCBINDING_FUNPTR,
  4987. NULL, NULL, only_flag);
  4988. /* Generate the symbols for the named constants representing
  4989. the kinds for intrinsic data types. */
  4990. for (i = 0; i < ISOCBINDING_NUMBER; i++)
  4991. {
  4992. bool found = false;
  4993. for (u = gfc_rename_list; u; u = u->next)
  4994. if (strcmp (c_interop_kinds_table[i].name, u->use_name) == 0)
  4995. {
  4996. bool not_in_std;
  4997. const char *name;
  4998. u->found = 1;
  4999. found = true;
  5000. switch (i)
  5001. {
  5002. #define NAMED_FUNCTION(a,b,c,d) \
  5003. case a: \
  5004. not_in_std = (gfc_option.allow_std & d) == 0; \
  5005. name = b; \
  5006. break;
  5007. #define NAMED_SUBROUTINE(a,b,c,d) \
  5008. case a: \
  5009. not_in_std = (gfc_option.allow_std & d) == 0; \
  5010. name = b; \
  5011. break;
  5012. #define NAMED_INTCST(a,b,c,d) \
  5013. case a: \
  5014. not_in_std = (gfc_option.allow_std & d) == 0; \
  5015. name = b; \
  5016. break;
  5017. #define NAMED_REALCST(a,b,c,d) \
  5018. case a: \
  5019. not_in_std = (gfc_option.allow_std & d) == 0; \
  5020. name = b; \
  5021. break;
  5022. #define NAMED_CMPXCST(a,b,c,d) \
  5023. case a: \
  5024. not_in_std = (gfc_option.allow_std & d) == 0; \
  5025. name = b; \
  5026. break;
  5027. #include "iso-c-binding.def"
  5028. default:
  5029. not_in_std = false;
  5030. name = "";
  5031. }
  5032. if (not_in_std)
  5033. {
  5034. gfc_error ("The symbol %qs, referenced at %L, is not "
  5035. "in the selected standard", name, &u->where);
  5036. continue;
  5037. }
  5038. switch (i)
  5039. {
  5040. #define NAMED_FUNCTION(a,b,c,d) \
  5041. case a: \
  5042. if (a == ISOCBINDING_LOC) \
  5043. return_type = c_ptr->n.sym; \
  5044. else if (a == ISOCBINDING_FUNLOC) \
  5045. return_type = c_funptr->n.sym; \
  5046. else \
  5047. return_type = NULL; \
  5048. create_intrinsic_function (u->local_name[0] \
  5049. ? u->local_name : u->use_name, \
  5050. a, iso_c_module_name, \
  5051. INTMOD_ISO_C_BINDING, false, \
  5052. return_type); \
  5053. break;
  5054. #define NAMED_SUBROUTINE(a,b,c,d) \
  5055. case a: \
  5056. create_intrinsic_function (u->local_name[0] ? u->local_name \
  5057. : u->use_name, \
  5058. a, iso_c_module_name, \
  5059. INTMOD_ISO_C_BINDING, true, NULL); \
  5060. break;
  5061. #include "iso-c-binding.def"
  5062. case ISOCBINDING_PTR:
  5063. case ISOCBINDING_FUNPTR:
  5064. /* Already handled above. */
  5065. break;
  5066. default:
  5067. if (i == ISOCBINDING_NULL_PTR)
  5068. tmp_symtree = c_ptr;
  5069. else if (i == ISOCBINDING_NULL_FUNPTR)
  5070. tmp_symtree = c_funptr;
  5071. else
  5072. tmp_symtree = NULL;
  5073. generate_isocbinding_symbol (iso_c_module_name,
  5074. (iso_c_binding_symbol) i,
  5075. u->local_name[0]
  5076. ? u->local_name : u->use_name,
  5077. tmp_symtree, false);
  5078. }
  5079. }
  5080. if (!found && !only_flag)
  5081. {
  5082. /* Skip, if the symbol is not in the enabled standard. */
  5083. switch (i)
  5084. {
  5085. #define NAMED_FUNCTION(a,b,c,d) \
  5086. case a: \
  5087. if ((gfc_option.allow_std & d) == 0) \
  5088. continue; \
  5089. break;
  5090. #define NAMED_SUBROUTINE(a,b,c,d) \
  5091. case a: \
  5092. if ((gfc_option.allow_std & d) == 0) \
  5093. continue; \
  5094. break;
  5095. #define NAMED_INTCST(a,b,c,d) \
  5096. case a: \
  5097. if ((gfc_option.allow_std & d) == 0) \
  5098. continue; \
  5099. break;
  5100. #define NAMED_REALCST(a,b,c,d) \
  5101. case a: \
  5102. if ((gfc_option.allow_std & d) == 0) \
  5103. continue; \
  5104. break;
  5105. #define NAMED_CMPXCST(a,b,c,d) \
  5106. case a: \
  5107. if ((gfc_option.allow_std & d) == 0) \
  5108. continue; \
  5109. break;
  5110. #include "iso-c-binding.def"
  5111. default:
  5112. ; /* Not GFC_STD_* versioned. */
  5113. }
  5114. switch (i)
  5115. {
  5116. #define NAMED_FUNCTION(a,b,c,d) \
  5117. case a: \
  5118. if (a == ISOCBINDING_LOC) \
  5119. return_type = c_ptr->n.sym; \
  5120. else if (a == ISOCBINDING_FUNLOC) \
  5121. return_type = c_funptr->n.sym; \
  5122. else \
  5123. return_type = NULL; \
  5124. create_intrinsic_function (b, a, iso_c_module_name, \
  5125. INTMOD_ISO_C_BINDING, false, \
  5126. return_type); \
  5127. break;
  5128. #define NAMED_SUBROUTINE(a,b,c,d) \
  5129. case a: \
  5130. create_intrinsic_function (b, a, iso_c_module_name, \
  5131. INTMOD_ISO_C_BINDING, true, NULL); \
  5132. break;
  5133. #include "iso-c-binding.def"
  5134. case ISOCBINDING_PTR:
  5135. case ISOCBINDING_FUNPTR:
  5136. /* Already handled above. */
  5137. break;
  5138. default:
  5139. if (i == ISOCBINDING_NULL_PTR)
  5140. tmp_symtree = c_ptr;
  5141. else if (i == ISOCBINDING_NULL_FUNPTR)
  5142. tmp_symtree = c_funptr;
  5143. else
  5144. tmp_symtree = NULL;
  5145. generate_isocbinding_symbol (iso_c_module_name,
  5146. (iso_c_binding_symbol) i, NULL,
  5147. tmp_symtree, false);
  5148. }
  5149. }
  5150. }
  5151. for (u = gfc_rename_list; u; u = u->next)
  5152. {
  5153. if (u->found)
  5154. continue;
  5155. gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
  5156. "module ISO_C_BINDING", u->use_name, &u->where);
  5157. }
  5158. }
  5159. /* Add an integer named constant from a given module. */
  5160. static void
  5161. create_int_parameter (const char *name, int value, const char *modname,
  5162. intmod_id module, int id)
  5163. {
  5164. gfc_symtree *tmp_symtree;
  5165. gfc_symbol *sym;
  5166. tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
  5167. if (tmp_symtree != NULL)
  5168. {
  5169. if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
  5170. return;
  5171. else
  5172. gfc_error ("Symbol %qs already declared", name);
  5173. }
  5174. gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
  5175. sym = tmp_symtree->n.sym;
  5176. sym->module = gfc_get_string (modname);
  5177. sym->attr.flavor = FL_PARAMETER;
  5178. sym->ts.type = BT_INTEGER;
  5179. sym->ts.kind = gfc_default_integer_kind;
  5180. sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL, value);
  5181. sym->attr.use_assoc = 1;
  5182. sym->from_intmod = module;
  5183. sym->intmod_sym_id = id;
  5184. }
  5185. /* Value is already contained by the array constructor, but not
  5186. yet the shape. */
  5187. static void
  5188. create_int_parameter_array (const char *name, int size, gfc_expr *value,
  5189. const char *modname, intmod_id module, int id)
  5190. {
  5191. gfc_symtree *tmp_symtree;
  5192. gfc_symbol *sym;
  5193. tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
  5194. if (tmp_symtree != NULL)
  5195. {
  5196. if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
  5197. return;
  5198. else
  5199. gfc_error ("Symbol %qs already declared", name);
  5200. }
  5201. gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
  5202. sym = tmp_symtree->n.sym;
  5203. sym->module = gfc_get_string (modname);
  5204. sym->attr.flavor = FL_PARAMETER;
  5205. sym->ts.type = BT_INTEGER;
  5206. sym->ts.kind = gfc_default_integer_kind;
  5207. sym->attr.use_assoc = 1;
  5208. sym->from_intmod = module;
  5209. sym->intmod_sym_id = id;
  5210. sym->attr.dimension = 1;
  5211. sym->as = gfc_get_array_spec ();
  5212. sym->as->rank = 1;
  5213. sym->as->type = AS_EXPLICIT;
  5214. sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
  5215. sym->as->upper[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, size);
  5216. sym->value = value;
  5217. sym->value->shape = gfc_get_shape (1);
  5218. mpz_init_set_ui (sym->value->shape[0], size);
  5219. }
  5220. /* Add an derived type for a given module. */
  5221. static void
  5222. create_derived_type (const char *name, const char *modname,
  5223. intmod_id module, int id)
  5224. {
  5225. gfc_symtree *tmp_symtree;
  5226. gfc_symbol *sym, *dt_sym;
  5227. gfc_interface *intr, *head;
  5228. tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
  5229. if (tmp_symtree != NULL)
  5230. {
  5231. if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
  5232. return;
  5233. else
  5234. gfc_error ("Symbol %qs already declared", name);
  5235. }
  5236. gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
  5237. sym = tmp_symtree->n.sym;
  5238. sym->module = gfc_get_string (modname);
  5239. sym->from_intmod = module;
  5240. sym->intmod_sym_id = id;
  5241. sym->attr.flavor = FL_PROCEDURE;
  5242. sym->attr.function = 1;
  5243. sym->attr.generic = 1;
  5244. gfc_get_sym_tree (dt_upper_string (sym->name),
  5245. gfc_current_ns, &tmp_symtree, false);
  5246. dt_sym = tmp_symtree->n.sym;
  5247. dt_sym->name = gfc_get_string (sym->name);
  5248. dt_sym->attr.flavor = FL_DERIVED;
  5249. dt_sym->attr.private_comp = 1;
  5250. dt_sym->attr.zero_comp = 1;
  5251. dt_sym->attr.use_assoc = 1;
  5252. dt_sym->module = gfc_get_string (modname);
  5253. dt_sym->from_intmod = module;
  5254. dt_sym->intmod_sym_id = id;
  5255. head = sym->generic;
  5256. intr = gfc_get_interface ();
  5257. intr->sym = dt_sym;
  5258. intr->where = gfc_current_locus;
  5259. intr->next = head;
  5260. sym->generic = intr;
  5261. sym->attr.if_source = IFSRC_DECL;
  5262. }
  5263. /* Read the contents of the module file into a temporary buffer. */
  5264. static void
  5265. read_module_to_tmpbuf ()
  5266. {
  5267. /* We don't know the uncompressed size, so enlarge the buffer as
  5268. needed. */
  5269. int cursz = 4096;
  5270. int rsize = cursz;
  5271. int len = 0;
  5272. module_content = XNEWVEC (char, cursz);
  5273. while (1)
  5274. {
  5275. int nread = gzread (module_fp, module_content + len, rsize);
  5276. len += nread;
  5277. if (nread < rsize)
  5278. break;
  5279. cursz *= 2;
  5280. module_content = XRESIZEVEC (char, module_content, cursz);
  5281. rsize = cursz - len;
  5282. }
  5283. module_content = XRESIZEVEC (char, module_content, len + 1);
  5284. module_content[len] = '\0';
  5285. module_pos = 0;
  5286. }
  5287. /* USE the ISO_FORTRAN_ENV intrinsic module. */
  5288. static void
  5289. use_iso_fortran_env_module (void)
  5290. {
  5291. static char mod[] = "iso_fortran_env";
  5292. gfc_use_rename *u;
  5293. gfc_symbol *mod_sym;
  5294. gfc_symtree *mod_symtree;
  5295. gfc_expr *expr;
  5296. int i, j;
  5297. intmod_sym symbol[] = {
  5298. #define NAMED_INTCST(a,b,c,d) { a, b, 0, d },
  5299. #define NAMED_KINDARRAY(a,b,c,d) { a, b, 0, d },
  5300. #define NAMED_DERIVED_TYPE(a,b,c,d) { a, b, 0, d },
  5301. #define NAMED_FUNCTION(a,b,c,d) { a, b, c, d },
  5302. #define NAMED_SUBROUTINE(a,b,c,d) { a, b, c, d },
  5303. #include "iso-fortran-env.def"
  5304. { ISOFORTRANENV_INVALID, NULL, -1234, 0 } };
  5305. i = 0;
  5306. #define NAMED_INTCST(a,b,c,d) symbol[i++].value = c;
  5307. #include "iso-fortran-env.def"
  5308. /* Generate the symbol for the module itself. */
  5309. mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, mod);
  5310. if (mod_symtree == NULL)
  5311. {
  5312. gfc_get_sym_tree (mod, gfc_current_ns, &mod_symtree, false);
  5313. gcc_assert (mod_symtree);
  5314. mod_sym = mod_symtree->n.sym;
  5315. mod_sym->attr.flavor = FL_MODULE;
  5316. mod_sym->attr.intrinsic = 1;
  5317. mod_sym->module = gfc_get_string (mod);
  5318. mod_sym->from_intmod = INTMOD_ISO_FORTRAN_ENV;
  5319. }
  5320. else
  5321. if (!mod_symtree->n.sym->attr.intrinsic)
  5322. gfc_error ("Use of intrinsic module %qs at %C conflicts with "
  5323. "non-intrinsic module name used previously", mod);
  5324. /* Generate the symbols for the module integer named constants. */
  5325. for (i = 0; symbol[i].name; i++)
  5326. {
  5327. bool found = false;
  5328. for (u = gfc_rename_list; u; u = u->next)
  5329. {
  5330. if (strcmp (symbol[i].name, u->use_name) == 0)
  5331. {
  5332. found = true;
  5333. u->found = 1;
  5334. if (!gfc_notify_std (symbol[i].standard, "The symbol %qs, "
  5335. "referenced at %L, is not in the selected "
  5336. "standard", symbol[i].name, &u->where))
  5337. continue;
  5338. if ((flag_default_integer || flag_default_real)
  5339. && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
  5340. gfc_warning_now (0, "Use of the NUMERIC_STORAGE_SIZE named "
  5341. "constant from intrinsic module "
  5342. "ISO_FORTRAN_ENV at %L is incompatible with "
  5343. "option %qs", &u->where,
  5344. flag_default_integer
  5345. ? "-fdefault-integer-8"
  5346. : "-fdefault-real-8");
  5347. switch (symbol[i].id)
  5348. {
  5349. #define NAMED_INTCST(a,b,c,d) \
  5350. case a:
  5351. #include "iso-fortran-env.def"
  5352. create_int_parameter (u->local_name[0] ? u->local_name
  5353. : u->use_name,
  5354. symbol[i].value, mod,
  5355. INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
  5356. break;
  5357. #define NAMED_KINDARRAY(a,b,KINDS,d) \
  5358. case a:\
  5359. expr = gfc_get_array_expr (BT_INTEGER, \
  5360. gfc_default_integer_kind,\
  5361. NULL); \
  5362. for (j = 0; KINDS[j].kind != 0; j++) \
  5363. gfc_constructor_append_expr (&expr->value.constructor, \
  5364. gfc_get_int_expr (gfc_default_integer_kind, NULL, \
  5365. KINDS[j].kind), NULL); \
  5366. create_int_parameter_array (u->local_name[0] ? u->local_name \
  5367. : u->use_name, \
  5368. j, expr, mod, \
  5369. INTMOD_ISO_FORTRAN_ENV, \
  5370. symbol[i].id); \
  5371. break;
  5372. #include "iso-fortran-env.def"
  5373. #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
  5374. case a:
  5375. #include "iso-fortran-env.def"
  5376. create_derived_type (u->local_name[0] ? u->local_name
  5377. : u->use_name,
  5378. mod, INTMOD_ISO_FORTRAN_ENV,
  5379. symbol[i].id);
  5380. break;
  5381. #define NAMED_FUNCTION(a,b,c,d) \
  5382. case a:
  5383. #include "iso-fortran-env.def"
  5384. create_intrinsic_function (u->local_name[0] ? u->local_name
  5385. : u->use_name,
  5386. symbol[i].id, mod,
  5387. INTMOD_ISO_FORTRAN_ENV, false,
  5388. NULL);
  5389. break;
  5390. default:
  5391. gcc_unreachable ();
  5392. }
  5393. }
  5394. }
  5395. if (!found && !only_flag)
  5396. {
  5397. if ((gfc_option.allow_std & symbol[i].standard) == 0)
  5398. continue;
  5399. if ((flag_default_integer || flag_default_real)
  5400. && symbol[i].id == ISOFORTRANENV_NUMERIC_STORAGE_SIZE)
  5401. gfc_warning_now (0,
  5402. "Use of the NUMERIC_STORAGE_SIZE named constant "
  5403. "from intrinsic module ISO_FORTRAN_ENV at %C is "
  5404. "incompatible with option %s",
  5405. flag_default_integer
  5406. ? "-fdefault-integer-8" : "-fdefault-real-8");
  5407. switch (symbol[i].id)
  5408. {
  5409. #define NAMED_INTCST(a,b,c,d) \
  5410. case a:
  5411. #include "iso-fortran-env.def"
  5412. create_int_parameter (symbol[i].name, symbol[i].value, mod,
  5413. INTMOD_ISO_FORTRAN_ENV, symbol[i].id);
  5414. break;
  5415. #define NAMED_KINDARRAY(a,b,KINDS,d) \
  5416. case a:\
  5417. expr = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, \
  5418. NULL); \
  5419. for (j = 0; KINDS[j].kind != 0; j++) \
  5420. gfc_constructor_append_expr (&expr->value.constructor, \
  5421. gfc_get_int_expr (gfc_default_integer_kind, NULL, \
  5422. KINDS[j].kind), NULL); \
  5423. create_int_parameter_array (symbol[i].name, j, expr, mod, \
  5424. INTMOD_ISO_FORTRAN_ENV, symbol[i].id);\
  5425. break;
  5426. #include "iso-fortran-env.def"
  5427. #define NAMED_DERIVED_TYPE(a,b,TYPE,STD) \
  5428. case a:
  5429. #include "iso-fortran-env.def"
  5430. create_derived_type (symbol[i].name, mod, INTMOD_ISO_FORTRAN_ENV,
  5431. symbol[i].id);
  5432. break;
  5433. #define NAMED_FUNCTION(a,b,c,d) \
  5434. case a:
  5435. #include "iso-fortran-env.def"
  5436. create_intrinsic_function (symbol[i].name, symbol[i].id, mod,
  5437. INTMOD_ISO_FORTRAN_ENV, false,
  5438. NULL);
  5439. break;
  5440. default:
  5441. gcc_unreachable ();
  5442. }
  5443. }
  5444. }
  5445. for (u = gfc_rename_list; u; u = u->next)
  5446. {
  5447. if (u->found)
  5448. continue;
  5449. gfc_error ("Symbol %qs referenced at %L not found in intrinsic "
  5450. "module ISO_FORTRAN_ENV", u->use_name, &u->where);
  5451. }
  5452. }
  5453. /* Process a USE directive. */
  5454. static void
  5455. gfc_use_module (gfc_use_list *module)
  5456. {
  5457. char *filename;
  5458. gfc_state_data *p;
  5459. int c, line, start;
  5460. gfc_symtree *mod_symtree;
  5461. gfc_use_list *use_stmt;
  5462. locus old_locus = gfc_current_locus;
  5463. gfc_current_locus = module->where;
  5464. module_name = module->module_name;
  5465. gfc_rename_list = module->rename;
  5466. only_flag = module->only_flag;
  5467. current_intmod = INTMOD_NONE;
  5468. if (!only_flag)
  5469. gfc_warning_now (OPT_Wuse_without_only,
  5470. "USE statement at %C has no ONLY qualifier");
  5471. filename = XALLOCAVEC (char, strlen (module_name) + strlen (MODULE_EXTENSION)
  5472. + 1);
  5473. strcpy (filename, module_name);
  5474. strcat (filename, MODULE_EXTENSION);
  5475. /* First, try to find an non-intrinsic module, unless the USE statement
  5476. specified that the module is intrinsic. */
  5477. module_fp = NULL;
  5478. if (!module->intrinsic)
  5479. module_fp = gzopen_included_file (filename, true, true);
  5480. /* Then, see if it's an intrinsic one, unless the USE statement
  5481. specified that the module is non-intrinsic. */
  5482. if (module_fp == NULL && !module->non_intrinsic)
  5483. {
  5484. if (strcmp (module_name, "iso_fortran_env") == 0
  5485. && gfc_notify_std (GFC_STD_F2003, "ISO_FORTRAN_ENV "
  5486. "intrinsic module at %C"))
  5487. {
  5488. use_iso_fortran_env_module ();
  5489. free_rename (module->rename);
  5490. module->rename = NULL;
  5491. gfc_current_locus = old_locus;
  5492. module->intrinsic = true;
  5493. return;
  5494. }
  5495. if (strcmp (module_name, "iso_c_binding") == 0
  5496. && gfc_notify_std (GFC_STD_F2003, "ISO_C_BINDING module at %C"))
  5497. {
  5498. import_iso_c_binding_module();
  5499. free_rename (module->rename);
  5500. module->rename = NULL;
  5501. gfc_current_locus = old_locus;
  5502. module->intrinsic = true;
  5503. return;
  5504. }
  5505. module_fp = gzopen_intrinsic_module (filename);
  5506. if (module_fp == NULL && module->intrinsic)
  5507. gfc_fatal_error ("Can't find an intrinsic module named %qs at %C",
  5508. module_name);
  5509. /* Check for the IEEE modules, so we can mark their symbols
  5510. accordingly when we read them. */
  5511. if (strcmp (module_name, "ieee_features") == 0
  5512. && gfc_notify_std (GFC_STD_F2003, "IEEE_FEATURES module at %C"))
  5513. {
  5514. current_intmod = INTMOD_IEEE_FEATURES;
  5515. }
  5516. else if (strcmp (module_name, "ieee_exceptions") == 0
  5517. && gfc_notify_std (GFC_STD_F2003,
  5518. "IEEE_EXCEPTIONS module at %C"))
  5519. {
  5520. current_intmod = INTMOD_IEEE_EXCEPTIONS;
  5521. }
  5522. else if (strcmp (module_name, "ieee_arithmetic") == 0
  5523. && gfc_notify_std (GFC_STD_F2003,
  5524. "IEEE_ARITHMETIC module at %C"))
  5525. {
  5526. current_intmod = INTMOD_IEEE_ARITHMETIC;
  5527. }
  5528. }
  5529. if (module_fp == NULL)
  5530. gfc_fatal_error ("Can't open module file %qs for reading at %C: %s",
  5531. filename, xstrerror (errno));
  5532. /* Check that we haven't already USEd an intrinsic module with the
  5533. same name. */
  5534. mod_symtree = gfc_find_symtree (gfc_current_ns->sym_root, module_name);
  5535. if (mod_symtree && mod_symtree->n.sym->attr.intrinsic)
  5536. gfc_error ("Use of non-intrinsic module %qs at %C conflicts with "
  5537. "intrinsic module name used previously", module_name);
  5538. iomode = IO_INPUT;
  5539. module_line = 1;
  5540. module_column = 1;
  5541. start = 0;
  5542. read_module_to_tmpbuf ();
  5543. gzclose (module_fp);
  5544. /* Skip the first line of the module, after checking that this is
  5545. a gfortran module file. */
  5546. line = 0;
  5547. while (line < 1)
  5548. {
  5549. c = module_char ();
  5550. if (c == EOF)
  5551. bad_module ("Unexpected end of module");
  5552. if (start++ < 3)
  5553. parse_name (c);
  5554. if ((start == 1 && strcmp (atom_name, "GFORTRAN") != 0)
  5555. || (start == 2 && strcmp (atom_name, " module") != 0))
  5556. gfc_fatal_error ("File %qs opened at %C is not a GNU Fortran"
  5557. " module file", filename);
  5558. if (start == 3)
  5559. {
  5560. if (strcmp (atom_name, " version") != 0
  5561. || module_char () != ' '
  5562. || parse_atom () != ATOM_STRING
  5563. || strcmp (atom_string, MOD_VERSION))
  5564. gfc_fatal_error ("Cannot read module file %qs opened at %C,"
  5565. " because it was created by a different"
  5566. " version of GNU Fortran", filename);
  5567. free (atom_string);
  5568. }
  5569. if (c == '\n')
  5570. line++;
  5571. }
  5572. /* Make sure we're not reading the same module that we may be building. */
  5573. for (p = gfc_state_stack; p; p = p->previous)
  5574. if (p->state == COMP_MODULE && strcmp (p->sym->name, module_name) == 0)
  5575. gfc_fatal_error ("Can't USE the same module we're building!");
  5576. init_pi_tree ();
  5577. init_true_name_tree ();
  5578. read_module ();
  5579. free_true_name (true_name_root);
  5580. true_name_root = NULL;
  5581. free_pi_tree (pi_root);
  5582. pi_root = NULL;
  5583. XDELETEVEC (module_content);
  5584. module_content = NULL;
  5585. use_stmt = gfc_get_use_list ();
  5586. *use_stmt = *module;
  5587. use_stmt->next = gfc_current_ns->use_stmts;
  5588. gfc_current_ns->use_stmts = use_stmt;
  5589. gfc_current_locus = old_locus;
  5590. }
  5591. /* Remove duplicated intrinsic operators from the rename list. */
  5592. static void
  5593. rename_list_remove_duplicate (gfc_use_rename *list)
  5594. {
  5595. gfc_use_rename *seek, *last;
  5596. for (; list; list = list->next)
  5597. if (list->op != INTRINSIC_USER && list->op != INTRINSIC_NONE)
  5598. {
  5599. last = list;
  5600. for (seek = list->next; seek; seek = last->next)
  5601. {
  5602. if (list->op == seek->op)
  5603. {
  5604. last->next = seek->next;
  5605. free (seek);
  5606. }
  5607. else
  5608. last = seek;
  5609. }
  5610. }
  5611. }
  5612. /* Process all USE directives. */
  5613. void
  5614. gfc_use_modules (void)
  5615. {
  5616. gfc_use_list *next, *seek, *last;
  5617. for (next = module_list; next; next = next->next)
  5618. {
  5619. bool non_intrinsic = next->non_intrinsic;
  5620. bool intrinsic = next->intrinsic;
  5621. bool neither = !non_intrinsic && !intrinsic;
  5622. for (seek = next->next; seek; seek = seek->next)
  5623. {
  5624. if (next->module_name != seek->module_name)
  5625. continue;
  5626. if (seek->non_intrinsic)
  5627. non_intrinsic = true;
  5628. else if (seek->intrinsic)
  5629. intrinsic = true;
  5630. else
  5631. neither = true;
  5632. }
  5633. if (intrinsic && neither && !non_intrinsic)
  5634. {
  5635. char *filename;
  5636. FILE *fp;
  5637. filename = XALLOCAVEC (char,
  5638. strlen (next->module_name)
  5639. + strlen (MODULE_EXTENSION) + 1);
  5640. strcpy (filename, next->module_name);
  5641. strcat (filename, MODULE_EXTENSION);
  5642. fp = gfc_open_included_file (filename, true, true);
  5643. if (fp != NULL)
  5644. {
  5645. non_intrinsic = true;
  5646. fclose (fp);
  5647. }
  5648. }
  5649. last = next;
  5650. for (seek = next->next; seek; seek = last->next)
  5651. {
  5652. if (next->module_name != seek->module_name)
  5653. {
  5654. last = seek;
  5655. continue;
  5656. }
  5657. if ((!next->intrinsic && !seek->intrinsic)
  5658. || (next->intrinsic && seek->intrinsic)
  5659. || !non_intrinsic)
  5660. {
  5661. if (!seek->only_flag)
  5662. next->only_flag = false;
  5663. if (seek->rename)
  5664. {
  5665. gfc_use_rename *r = seek->rename;
  5666. while (r->next)
  5667. r = r->next;
  5668. r->next = next->rename;
  5669. next->rename = seek->rename;
  5670. }
  5671. last->next = seek->next;
  5672. free (seek);
  5673. }
  5674. else
  5675. last = seek;
  5676. }
  5677. }
  5678. for (; module_list; module_list = next)
  5679. {
  5680. next = module_list->next;
  5681. rename_list_remove_duplicate (module_list->rename);
  5682. gfc_use_module (module_list);
  5683. free (module_list);
  5684. }
  5685. gfc_rename_list = NULL;
  5686. }
  5687. void
  5688. gfc_free_use_stmts (gfc_use_list *use_stmts)
  5689. {
  5690. gfc_use_list *next;
  5691. for (; use_stmts; use_stmts = next)
  5692. {
  5693. gfc_use_rename *next_rename;
  5694. for (; use_stmts->rename; use_stmts->rename = next_rename)
  5695. {
  5696. next_rename = use_stmts->rename->next;
  5697. free (use_stmts->rename);
  5698. }
  5699. next = use_stmts->next;
  5700. free (use_stmts);
  5701. }
  5702. }
  5703. void
  5704. gfc_module_init_2 (void)
  5705. {
  5706. last_atom = ATOM_LPAREN;
  5707. gfc_rename_list = NULL;
  5708. module_list = NULL;
  5709. }
  5710. void
  5711. gfc_module_done_2 (void)
  5712. {
  5713. free_rename (gfc_rename_list);
  5714. gfc_rename_list = NULL;
  5715. }