check.c 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260
  1. /* Check functions
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Andy Vaught & Katherine Holcomb
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* These functions check to see if an argument list is compatible with
  17. a particular intrinsic function or subroutine. Presence of
  18. required arguments has already been established, the argument list
  19. has been sorted into the right order and has NULL arguments in the
  20. correct places for missing optional arguments. */
  21. #include "config.h"
  22. #include "system.h"
  23. #include "coretypes.h"
  24. #include "flags.h"
  25. #include "gfortran.h"
  26. #include "intrinsic.h"
  27. #include "constructor.h"
  28. #include "target-memory.h"
  29. /* Make sure an expression is a scalar. */
  30. static bool
  31. scalar_check (gfc_expr *e, int n)
  32. {
  33. if (e->rank == 0)
  34. return true;
  35. gfc_error ("%qs argument of %qs intrinsic at %L must be a scalar",
  36. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  37. &e->where);
  38. return false;
  39. }
  40. /* Check the type of an expression. */
  41. static bool
  42. type_check (gfc_expr *e, int n, bt type)
  43. {
  44. if (e->ts.type == type)
  45. return true;
  46. gfc_error ("%qs argument of %qs intrinsic at %L must be %s",
  47. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  48. &e->where, gfc_basic_typename (type));
  49. return false;
  50. }
  51. /* Check that the expression is a numeric type. */
  52. static bool
  53. numeric_check (gfc_expr *e, int n)
  54. {
  55. if (gfc_numeric_ts (&e->ts))
  56. return true;
  57. /* If the expression has not got a type, check if its namespace can
  58. offer a default type. */
  59. if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
  60. && e->symtree->n.sym->ts.type == BT_UNKNOWN
  61. && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
  62. && gfc_numeric_ts (&e->symtree->n.sym->ts))
  63. {
  64. e->ts = e->symtree->n.sym->ts;
  65. return true;
  66. }
  67. gfc_error ("%qs argument of %qs intrinsic at %L must be a numeric type",
  68. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  69. &e->where);
  70. return false;
  71. }
  72. /* Check that an expression is integer or real. */
  73. static bool
  74. int_or_real_check (gfc_expr *e, int n)
  75. {
  76. if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
  77. {
  78. gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
  79. "or REAL", gfc_current_intrinsic_arg[n]->name,
  80. gfc_current_intrinsic, &e->where);
  81. return false;
  82. }
  83. return true;
  84. }
  85. /* Check that an expression is real or complex. */
  86. static bool
  87. real_or_complex_check (gfc_expr *e, int n)
  88. {
  89. if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
  90. {
  91. gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
  92. "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
  93. gfc_current_intrinsic, &e->where);
  94. return false;
  95. }
  96. return true;
  97. }
  98. /* Check that an expression is INTEGER or PROCEDURE. */
  99. static bool
  100. int_or_proc_check (gfc_expr *e, int n)
  101. {
  102. if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
  103. {
  104. gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
  105. "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
  106. gfc_current_intrinsic, &e->where);
  107. return false;
  108. }
  109. return true;
  110. }
  111. /* Check that the expression is an optional constant integer
  112. and that it specifies a valid kind for that type. */
  113. static bool
  114. kind_check (gfc_expr *k, int n, bt type)
  115. {
  116. int kind;
  117. if (k == NULL)
  118. return true;
  119. if (!type_check (k, n, BT_INTEGER))
  120. return false;
  121. if (!scalar_check (k, n))
  122. return false;
  123. if (!gfc_check_init_expr (k))
  124. {
  125. gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
  126. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  127. &k->where);
  128. return false;
  129. }
  130. if (gfc_extract_int (k, &kind) != NULL
  131. || gfc_validate_kind (type, kind, true) < 0)
  132. {
  133. gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
  134. &k->where);
  135. return false;
  136. }
  137. return true;
  138. }
  139. /* Make sure the expression is a double precision real. */
  140. static bool
  141. double_check (gfc_expr *d, int n)
  142. {
  143. if (!type_check (d, n, BT_REAL))
  144. return false;
  145. if (d->ts.kind != gfc_default_double_kind)
  146. {
  147. gfc_error ("%qs argument of %qs intrinsic at %L must be double "
  148. "precision", gfc_current_intrinsic_arg[n]->name,
  149. gfc_current_intrinsic, &d->where);
  150. return false;
  151. }
  152. return true;
  153. }
  154. static bool
  155. coarray_check (gfc_expr *e, int n)
  156. {
  157. if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
  158. && CLASS_DATA (e)->attr.codimension
  159. && CLASS_DATA (e)->as->corank)
  160. {
  161. gfc_add_class_array_ref (e);
  162. return true;
  163. }
  164. if (!gfc_is_coarray (e))
  165. {
  166. gfc_error ("Expected coarray variable as %qs argument to the %s "
  167. "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
  168. gfc_current_intrinsic, &e->where);
  169. return false;
  170. }
  171. return true;
  172. }
  173. /* Make sure the expression is a logical array. */
  174. static bool
  175. logical_array_check (gfc_expr *array, int n)
  176. {
  177. if (array->ts.type != BT_LOGICAL || array->rank == 0)
  178. {
  179. gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
  180. "array", gfc_current_intrinsic_arg[n]->name,
  181. gfc_current_intrinsic, &array->where);
  182. return false;
  183. }
  184. return true;
  185. }
  186. /* Make sure an expression is an array. */
  187. static bool
  188. array_check (gfc_expr *e, int n)
  189. {
  190. if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
  191. && CLASS_DATA (e)->attr.dimension
  192. && CLASS_DATA (e)->as->rank)
  193. {
  194. gfc_add_class_array_ref (e);
  195. return true;
  196. }
  197. if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
  198. return true;
  199. gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
  200. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  201. &e->where);
  202. return false;
  203. }
  204. /* If expr is a constant, then check to ensure that it is greater than
  205. of equal to zero. */
  206. static bool
  207. nonnegative_check (const char *arg, gfc_expr *expr)
  208. {
  209. int i;
  210. if (expr->expr_type == EXPR_CONSTANT)
  211. {
  212. gfc_extract_int (expr, &i);
  213. if (i < 0)
  214. {
  215. gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. /* If expr2 is constant, then check that the value is less than
  222. (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
  223. static bool
  224. less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
  225. gfc_expr *expr2, bool or_equal)
  226. {
  227. int i2, i3;
  228. if (expr2->expr_type == EXPR_CONSTANT)
  229. {
  230. gfc_extract_int (expr2, &i2);
  231. i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
  232. /* For ISHFT[C], check that |shift| <= bit_size(i). */
  233. if (arg2 == NULL)
  234. {
  235. if (i2 < 0)
  236. i2 = -i2;
  237. if (i2 > gfc_integer_kinds[i3].bit_size)
  238. {
  239. gfc_error ("The absolute value of SHIFT at %L must be less "
  240. "than or equal to BIT_SIZE(%qs)",
  241. &expr2->where, arg1);
  242. return false;
  243. }
  244. }
  245. if (or_equal)
  246. {
  247. if (i2 > gfc_integer_kinds[i3].bit_size)
  248. {
  249. gfc_error ("%qs at %L must be less than "
  250. "or equal to BIT_SIZE(%qs)",
  251. arg2, &expr2->where, arg1);
  252. return false;
  253. }
  254. }
  255. else
  256. {
  257. if (i2 >= gfc_integer_kinds[i3].bit_size)
  258. {
  259. gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
  260. arg2, &expr2->where, arg1);
  261. return false;
  262. }
  263. }
  264. }
  265. return true;
  266. }
  267. /* If expr is constant, then check that the value is less than or equal
  268. to the bit_size of the kind k. */
  269. static bool
  270. less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
  271. {
  272. int i, val;
  273. if (expr->expr_type != EXPR_CONSTANT)
  274. return true;
  275. i = gfc_validate_kind (BT_INTEGER, k, false);
  276. gfc_extract_int (expr, &val);
  277. if (val > gfc_integer_kinds[i].bit_size)
  278. {
  279. gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
  280. "INTEGER(KIND=%d)", arg, &expr->where, k);
  281. return false;
  282. }
  283. return true;
  284. }
  285. /* If expr2 and expr3 are constants, then check that the value is less than
  286. or equal to bit_size(expr1). */
  287. static bool
  288. less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
  289. gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
  290. {
  291. int i2, i3;
  292. if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
  293. {
  294. gfc_extract_int (expr2, &i2);
  295. gfc_extract_int (expr3, &i3);
  296. i2 += i3;
  297. i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
  298. if (i2 > gfc_integer_kinds[i3].bit_size)
  299. {
  300. gfc_error ("%<%s + %s%> at %L must be less than or equal "
  301. "to BIT_SIZE(%qs)",
  302. arg2, arg3, &expr2->where, arg1);
  303. return false;
  304. }
  305. }
  306. return true;
  307. }
  308. /* Make sure two expressions have the same type. */
  309. static bool
  310. same_type_check (gfc_expr *e, int n, gfc_expr *f, int m)
  311. {
  312. if (gfc_compare_types (&e->ts, &f->ts))
  313. return true;
  314. gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
  315. "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
  316. gfc_current_intrinsic, &f->where,
  317. gfc_current_intrinsic_arg[n]->name);
  318. return false;
  319. }
  320. /* Make sure that an expression has a certain (nonzero) rank. */
  321. static bool
  322. rank_check (gfc_expr *e, int n, int rank)
  323. {
  324. if (e->rank == rank)
  325. return true;
  326. gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
  327. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  328. &e->where, rank);
  329. return false;
  330. }
  331. /* Make sure a variable expression is not an optional dummy argument. */
  332. static bool
  333. nonoptional_check (gfc_expr *e, int n)
  334. {
  335. if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
  336. {
  337. gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
  338. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  339. &e->where);
  340. }
  341. /* TODO: Recursive check on nonoptional variables? */
  342. return true;
  343. }
  344. /* Check for ALLOCATABLE attribute. */
  345. static bool
  346. allocatable_check (gfc_expr *e, int n)
  347. {
  348. symbol_attribute attr;
  349. attr = gfc_variable_attr (e, NULL);
  350. if (!attr.allocatable || attr.associate_var)
  351. {
  352. gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
  353. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  354. &e->where);
  355. return false;
  356. }
  357. return true;
  358. }
  359. /* Check that an expression has a particular kind. */
  360. static bool
  361. kind_value_check (gfc_expr *e, int n, int k)
  362. {
  363. if (e->ts.kind == k)
  364. return true;
  365. gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
  366. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
  367. &e->where, k);
  368. return false;
  369. }
  370. /* Make sure an expression is a variable. */
  371. static bool
  372. variable_check (gfc_expr *e, int n, bool allow_proc)
  373. {
  374. if (e->expr_type == EXPR_VARIABLE
  375. && e->symtree->n.sym->attr.intent == INTENT_IN
  376. && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
  377. || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
  378. {
  379. gfc_ref *ref;
  380. bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
  381. && CLASS_DATA (e->symtree->n.sym)
  382. ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
  383. : e->symtree->n.sym->attr.pointer;
  384. for (ref = e->ref; ref; ref = ref->next)
  385. {
  386. if (pointer && ref->type == REF_COMPONENT)
  387. break;
  388. if (ref->type == REF_COMPONENT
  389. && ((ref->u.c.component->ts.type == BT_CLASS
  390. && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
  391. || (ref->u.c.component->ts.type != BT_CLASS
  392. && ref->u.c.component->attr.pointer)))
  393. break;
  394. }
  395. if (!ref)
  396. {
  397. gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
  398. "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
  399. gfc_current_intrinsic, &e->where);
  400. return false;
  401. }
  402. }
  403. if (e->expr_type == EXPR_VARIABLE
  404. && e->symtree->n.sym->attr.flavor != FL_PARAMETER
  405. && (allow_proc || !e->symtree->n.sym->attr.function))
  406. return true;
  407. if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
  408. && e->symtree->n.sym == e->symtree->n.sym->result)
  409. {
  410. gfc_namespace *ns;
  411. for (ns = gfc_current_ns; ns; ns = ns->parent)
  412. if (ns->proc_name == e->symtree->n.sym)
  413. return true;
  414. }
  415. gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
  416. gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
  417. return false;
  418. }
  419. /* Check the common DIM parameter for correctness. */
  420. static bool
  421. dim_check (gfc_expr *dim, int n, bool optional)
  422. {
  423. if (dim == NULL)
  424. return true;
  425. if (!type_check (dim, n, BT_INTEGER))
  426. return false;
  427. if (!scalar_check (dim, n))
  428. return false;
  429. if (!optional && !nonoptional_check (dim, n))
  430. return false;
  431. return true;
  432. }
  433. /* If a coarray DIM parameter is a constant, make sure that it is greater than
  434. zero and less than or equal to the corank of the given array. */
  435. static bool
  436. dim_corank_check (gfc_expr *dim, gfc_expr *array)
  437. {
  438. int corank;
  439. gcc_assert (array->expr_type == EXPR_VARIABLE);
  440. if (dim->expr_type != EXPR_CONSTANT)
  441. return true;
  442. if (array->ts.type == BT_CLASS)
  443. return true;
  444. corank = gfc_get_corank (array);
  445. if (mpz_cmp_ui (dim->value.integer, 1) < 0
  446. || mpz_cmp_ui (dim->value.integer, corank) > 0)
  447. {
  448. gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
  449. "codimension index", gfc_current_intrinsic, &dim->where);
  450. return false;
  451. }
  452. return true;
  453. }
  454. /* If a DIM parameter is a constant, make sure that it is greater than
  455. zero and less than or equal to the rank of the given array. If
  456. allow_assumed is zero then dim must be less than the rank of the array
  457. for assumed size arrays. */
  458. static bool
  459. dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
  460. {
  461. gfc_array_ref *ar;
  462. int rank;
  463. if (dim == NULL)
  464. return true;
  465. if (dim->expr_type != EXPR_CONSTANT)
  466. return true;
  467. if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
  468. && array->value.function.isym->id == GFC_ISYM_SPREAD)
  469. rank = array->rank + 1;
  470. else
  471. rank = array->rank;
  472. /* Assumed-rank array. */
  473. if (rank == -1)
  474. rank = GFC_MAX_DIMENSIONS;
  475. if (array->expr_type == EXPR_VARIABLE)
  476. {
  477. ar = gfc_find_array_ref (array);
  478. if (ar->as->type == AS_ASSUMED_SIZE
  479. && !allow_assumed
  480. && ar->type != AR_ELEMENT
  481. && ar->type != AR_SECTION)
  482. rank--;
  483. }
  484. if (mpz_cmp_ui (dim->value.integer, 1) < 0
  485. || mpz_cmp_ui (dim->value.integer, rank) > 0)
  486. {
  487. gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
  488. "dimension index", gfc_current_intrinsic, &dim->where);
  489. return false;
  490. }
  491. return true;
  492. }
  493. /* Compare the size of a along dimension ai with the size of b along
  494. dimension bi, returning 0 if they are known not to be identical,
  495. and 1 if they are identical, or if this cannot be determined. */
  496. static int
  497. identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
  498. {
  499. mpz_t a_size, b_size;
  500. int ret;
  501. gcc_assert (a->rank > ai);
  502. gcc_assert (b->rank > bi);
  503. ret = 1;
  504. if (gfc_array_dimen_size (a, ai, &a_size))
  505. {
  506. if (gfc_array_dimen_size (b, bi, &b_size))
  507. {
  508. if (mpz_cmp (a_size, b_size) != 0)
  509. ret = 0;
  510. mpz_clear (b_size);
  511. }
  512. mpz_clear (a_size);
  513. }
  514. return ret;
  515. }
  516. /* Calculate the length of a character variable, including substrings.
  517. Strip away parentheses if necessary. Return -1 if no length could
  518. be determined. */
  519. static long
  520. gfc_var_strlen (const gfc_expr *a)
  521. {
  522. gfc_ref *ra;
  523. while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
  524. a = a->value.op.op1;
  525. for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
  526. ;
  527. if (ra)
  528. {
  529. long start_a, end_a;
  530. if (!ra->u.ss.end)
  531. return -1;
  532. if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
  533. && ra->u.ss.end->expr_type == EXPR_CONSTANT)
  534. {
  535. start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
  536. : 1;
  537. end_a = mpz_get_si (ra->u.ss.end->value.integer);
  538. return (end_a < start_a) ? 0 : end_a - start_a + 1;
  539. }
  540. else if (ra->u.ss.start
  541. && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
  542. return 1;
  543. else
  544. return -1;
  545. }
  546. if (a->ts.u.cl && a->ts.u.cl->length
  547. && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
  548. return mpz_get_si (a->ts.u.cl->length->value.integer);
  549. else if (a->expr_type == EXPR_CONSTANT
  550. && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
  551. return a->value.character.length;
  552. else
  553. return -1;
  554. }
  555. /* Check whether two character expressions have the same length;
  556. returns true if they have or if the length cannot be determined,
  557. otherwise return false and raise a gfc_error. */
  558. bool
  559. gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
  560. {
  561. long len_a, len_b;
  562. len_a = gfc_var_strlen(a);
  563. len_b = gfc_var_strlen(b);
  564. if (len_a == -1 || len_b == -1 || len_a == len_b)
  565. return true;
  566. else
  567. {
  568. gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
  569. len_a, len_b, name, &a->where);
  570. return false;
  571. }
  572. }
  573. /***** Check functions *****/
  574. /* Check subroutine suitable for intrinsics taking a real argument and
  575. a kind argument for the result. */
  576. static bool
  577. check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
  578. {
  579. if (!type_check (a, 0, BT_REAL))
  580. return false;
  581. if (!kind_check (kind, 1, type))
  582. return false;
  583. return true;
  584. }
  585. /* Check subroutine suitable for ceiling, floor and nint. */
  586. bool
  587. gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
  588. {
  589. return check_a_kind (a, kind, BT_INTEGER);
  590. }
  591. /* Check subroutine suitable for aint, anint. */
  592. bool
  593. gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
  594. {
  595. return check_a_kind (a, kind, BT_REAL);
  596. }
  597. bool
  598. gfc_check_abs (gfc_expr *a)
  599. {
  600. if (!numeric_check (a, 0))
  601. return false;
  602. return true;
  603. }
  604. bool
  605. gfc_check_achar (gfc_expr *a, gfc_expr *kind)
  606. {
  607. if (!type_check (a, 0, BT_INTEGER))
  608. return false;
  609. if (!kind_check (kind, 1, BT_CHARACTER))
  610. return false;
  611. return true;
  612. }
  613. bool
  614. gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
  615. {
  616. if (!type_check (name, 0, BT_CHARACTER)
  617. || !scalar_check (name, 0))
  618. return false;
  619. if (!kind_value_check (name, 0, gfc_default_character_kind))
  620. return false;
  621. if (!type_check (mode, 1, BT_CHARACTER)
  622. || !scalar_check (mode, 1))
  623. return false;
  624. if (!kind_value_check (mode, 1, gfc_default_character_kind))
  625. return false;
  626. return true;
  627. }
  628. bool
  629. gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
  630. {
  631. if (!logical_array_check (mask, 0))
  632. return false;
  633. if (!dim_check (dim, 1, false))
  634. return false;
  635. if (!dim_rank_check (dim, mask, 0))
  636. return false;
  637. return true;
  638. }
  639. bool
  640. gfc_check_allocated (gfc_expr *array)
  641. {
  642. if (!variable_check (array, 0, false))
  643. return false;
  644. if (!allocatable_check (array, 0))
  645. return false;
  646. return true;
  647. }
  648. /* Common check function where the first argument must be real or
  649. integer and the second argument must be the same as the first. */
  650. bool
  651. gfc_check_a_p (gfc_expr *a, gfc_expr *p)
  652. {
  653. if (!int_or_real_check (a, 0))
  654. return false;
  655. if (a->ts.type != p->ts.type)
  656. {
  657. gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
  658. "have the same type", gfc_current_intrinsic_arg[0]->name,
  659. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  660. &p->where);
  661. return false;
  662. }
  663. if (a->ts.kind != p->ts.kind)
  664. {
  665. if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
  666. &p->where))
  667. return false;
  668. }
  669. return true;
  670. }
  671. bool
  672. gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
  673. {
  674. if (!double_check (x, 0) || !double_check (y, 1))
  675. return false;
  676. return true;
  677. }
  678. bool
  679. gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
  680. {
  681. symbol_attribute attr1, attr2;
  682. int i;
  683. bool t;
  684. locus *where;
  685. where = &pointer->where;
  686. if (pointer->expr_type == EXPR_NULL)
  687. goto null_arg;
  688. attr1 = gfc_expr_attr (pointer);
  689. if (!attr1.pointer && !attr1.proc_pointer)
  690. {
  691. gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
  692. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  693. &pointer->where);
  694. return false;
  695. }
  696. /* F2008, C1242. */
  697. if (attr1.pointer && gfc_is_coindexed (pointer))
  698. {
  699. gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
  700. "coindexed", gfc_current_intrinsic_arg[0]->name,
  701. gfc_current_intrinsic, &pointer->where);
  702. return false;
  703. }
  704. /* Target argument is optional. */
  705. if (target == NULL)
  706. return true;
  707. where = &target->where;
  708. if (target->expr_type == EXPR_NULL)
  709. goto null_arg;
  710. if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
  711. attr2 = gfc_expr_attr (target);
  712. else
  713. {
  714. gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
  715. "or target VARIABLE or FUNCTION",
  716. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  717. &target->where);
  718. return false;
  719. }
  720. if (attr1.pointer && !attr2.pointer && !attr2.target)
  721. {
  722. gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
  723. "or a TARGET", gfc_current_intrinsic_arg[1]->name,
  724. gfc_current_intrinsic, &target->where);
  725. return false;
  726. }
  727. /* F2008, C1242. */
  728. if (attr1.pointer && gfc_is_coindexed (target))
  729. {
  730. gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
  731. "coindexed", gfc_current_intrinsic_arg[1]->name,
  732. gfc_current_intrinsic, &target->where);
  733. return false;
  734. }
  735. t = true;
  736. if (!same_type_check (pointer, 0, target, 1))
  737. t = false;
  738. if (!rank_check (target, 0, pointer->rank))
  739. t = false;
  740. if (target->rank > 0)
  741. {
  742. for (i = 0; i < target->rank; i++)
  743. if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
  744. {
  745. gfc_error ("Array section with a vector subscript at %L shall not "
  746. "be the target of a pointer",
  747. &target->where);
  748. t = false;
  749. break;
  750. }
  751. }
  752. return t;
  753. null_arg:
  754. gfc_error ("NULL pointer at %L is not permitted as actual argument "
  755. "of %qs intrinsic function", where, gfc_current_intrinsic);
  756. return false;
  757. }
  758. bool
  759. gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
  760. {
  761. /* gfc_notify_std would be a waste of time as the return value
  762. is seemingly used only for the generic resolution. The error
  763. will be: Too many arguments. */
  764. if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
  765. return false;
  766. return gfc_check_atan2 (y, x);
  767. }
  768. bool
  769. gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
  770. {
  771. if (!type_check (y, 0, BT_REAL))
  772. return false;
  773. if (!same_type_check (y, 0, x, 1))
  774. return false;
  775. return true;
  776. }
  777. static bool
  778. gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
  779. gfc_expr *stat, int stat_no)
  780. {
  781. if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
  782. return false;
  783. if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
  784. && !(atom->ts.type == BT_LOGICAL
  785. && atom->ts.kind == gfc_atomic_logical_kind))
  786. {
  787. gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
  788. "integer of ATOMIC_INT_KIND or a logical of "
  789. "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
  790. return false;
  791. }
  792. if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
  793. {
  794. gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
  795. "coarray or coindexed", &atom->where, gfc_current_intrinsic);
  796. return false;
  797. }
  798. if (atom->ts.type != value->ts.type)
  799. {
  800. gfc_error_1 ("'%s' argument of '%s' intrinsic at %L shall have the same "
  801. "type as '%s' at %L", gfc_current_intrinsic_arg[val_no]->name,
  802. gfc_current_intrinsic, &value->where,
  803. gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
  804. return false;
  805. }
  806. if (stat != NULL)
  807. {
  808. if (!type_check (stat, stat_no, BT_INTEGER))
  809. return false;
  810. if (!scalar_check (stat, stat_no))
  811. return false;
  812. if (!variable_check (stat, stat_no, false))
  813. return false;
  814. if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
  815. return false;
  816. if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
  817. gfc_current_intrinsic, &stat->where))
  818. return false;
  819. }
  820. return true;
  821. }
  822. bool
  823. gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
  824. {
  825. if (atom->expr_type == EXPR_FUNCTION
  826. && atom->value.function.isym
  827. && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
  828. atom = atom->value.function.actual->expr;
  829. if (!gfc_check_vardef_context (atom, false, false, false, NULL))
  830. {
  831. gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
  832. "definable", gfc_current_intrinsic, &atom->where);
  833. return false;
  834. }
  835. return gfc_check_atomic (atom, 0, value, 1, stat, 2);
  836. }
  837. bool
  838. gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
  839. {
  840. if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
  841. {
  842. gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
  843. "integer of ATOMIC_INT_KIND", &atom->where,
  844. gfc_current_intrinsic);
  845. return false;
  846. }
  847. return gfc_check_atomic_def (atom, value, stat);
  848. }
  849. bool
  850. gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
  851. {
  852. if (atom->expr_type == EXPR_FUNCTION
  853. && atom->value.function.isym
  854. && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
  855. atom = atom->value.function.actual->expr;
  856. if (!gfc_check_vardef_context (value, false, false, false, NULL))
  857. {
  858. gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
  859. "definable", gfc_current_intrinsic, &value->where);
  860. return false;
  861. }
  862. return gfc_check_atomic (atom, 1, value, 0, stat, 2);
  863. }
  864. bool
  865. gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
  866. gfc_expr *new_val, gfc_expr *stat)
  867. {
  868. if (atom->expr_type == EXPR_FUNCTION
  869. && atom->value.function.isym
  870. && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
  871. atom = atom->value.function.actual->expr;
  872. if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
  873. return false;
  874. if (!scalar_check (old, 1) || !scalar_check (compare, 2))
  875. return false;
  876. if (!same_type_check (atom, 0, old, 1))
  877. return false;
  878. if (!same_type_check (atom, 0, compare, 2))
  879. return false;
  880. if (!gfc_check_vardef_context (atom, false, false, false, NULL))
  881. {
  882. gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
  883. "definable", gfc_current_intrinsic, &atom->where);
  884. return false;
  885. }
  886. if (!gfc_check_vardef_context (old, false, false, false, NULL))
  887. {
  888. gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
  889. "definable", gfc_current_intrinsic, &old->where);
  890. return false;
  891. }
  892. return true;
  893. }
  894. bool
  895. gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
  896. gfc_expr *stat)
  897. {
  898. if (atom->expr_type == EXPR_FUNCTION
  899. && atom->value.function.isym
  900. && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
  901. atom = atom->value.function.actual->expr;
  902. if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
  903. {
  904. gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
  905. "integer of ATOMIC_INT_KIND", &atom->where,
  906. gfc_current_intrinsic);
  907. return false;
  908. }
  909. if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
  910. return false;
  911. if (!scalar_check (old, 2))
  912. return false;
  913. if (!same_type_check (atom, 0, old, 2))
  914. return false;
  915. if (!gfc_check_vardef_context (atom, false, false, false, NULL))
  916. {
  917. gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
  918. "definable", gfc_current_intrinsic, &atom->where);
  919. return false;
  920. }
  921. if (!gfc_check_vardef_context (old, false, false, false, NULL))
  922. {
  923. gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
  924. "definable", gfc_current_intrinsic, &old->where);
  925. return false;
  926. }
  927. return true;
  928. }
  929. /* BESJN and BESYN functions. */
  930. bool
  931. gfc_check_besn (gfc_expr *n, gfc_expr *x)
  932. {
  933. if (!type_check (n, 0, BT_INTEGER))
  934. return false;
  935. if (n->expr_type == EXPR_CONSTANT)
  936. {
  937. int i;
  938. gfc_extract_int (n, &i);
  939. if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
  940. "N at %L", &n->where))
  941. return false;
  942. }
  943. if (!type_check (x, 1, BT_REAL))
  944. return false;
  945. return true;
  946. }
  947. /* Transformational version of the Bessel JN and YN functions. */
  948. bool
  949. gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
  950. {
  951. if (!type_check (n1, 0, BT_INTEGER))
  952. return false;
  953. if (!scalar_check (n1, 0))
  954. return false;
  955. if (!nonnegative_check ("N1", n1))
  956. return false;
  957. if (!type_check (n2, 1, BT_INTEGER))
  958. return false;
  959. if (!scalar_check (n2, 1))
  960. return false;
  961. if (!nonnegative_check ("N2", n2))
  962. return false;
  963. if (!type_check (x, 2, BT_REAL))
  964. return false;
  965. if (!scalar_check (x, 2))
  966. return false;
  967. return true;
  968. }
  969. bool
  970. gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
  971. {
  972. if (!type_check (i, 0, BT_INTEGER))
  973. return false;
  974. if (!type_check (j, 1, BT_INTEGER))
  975. return false;
  976. return true;
  977. }
  978. bool
  979. gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
  980. {
  981. if (!type_check (i, 0, BT_INTEGER))
  982. return false;
  983. if (!type_check (pos, 1, BT_INTEGER))
  984. return false;
  985. if (!nonnegative_check ("pos", pos))
  986. return false;
  987. if (!less_than_bitsize1 ("i", i, "pos", pos, false))
  988. return false;
  989. return true;
  990. }
  991. bool
  992. gfc_check_char (gfc_expr *i, gfc_expr *kind)
  993. {
  994. if (!type_check (i, 0, BT_INTEGER))
  995. return false;
  996. if (!kind_check (kind, 1, BT_CHARACTER))
  997. return false;
  998. return true;
  999. }
  1000. bool
  1001. gfc_check_chdir (gfc_expr *dir)
  1002. {
  1003. if (!type_check (dir, 0, BT_CHARACTER))
  1004. return false;
  1005. if (!kind_value_check (dir, 0, gfc_default_character_kind))
  1006. return false;
  1007. return true;
  1008. }
  1009. bool
  1010. gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
  1011. {
  1012. if (!type_check (dir, 0, BT_CHARACTER))
  1013. return false;
  1014. if (!kind_value_check (dir, 0, gfc_default_character_kind))
  1015. return false;
  1016. if (status == NULL)
  1017. return true;
  1018. if (!type_check (status, 1, BT_INTEGER))
  1019. return false;
  1020. if (!scalar_check (status, 1))
  1021. return false;
  1022. return true;
  1023. }
  1024. bool
  1025. gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
  1026. {
  1027. if (!type_check (name, 0, BT_CHARACTER))
  1028. return false;
  1029. if (!kind_value_check (name, 0, gfc_default_character_kind))
  1030. return false;
  1031. if (!type_check (mode, 1, BT_CHARACTER))
  1032. return false;
  1033. if (!kind_value_check (mode, 1, gfc_default_character_kind))
  1034. return false;
  1035. return true;
  1036. }
  1037. bool
  1038. gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
  1039. {
  1040. if (!type_check (name, 0, BT_CHARACTER))
  1041. return false;
  1042. if (!kind_value_check (name, 0, gfc_default_character_kind))
  1043. return false;
  1044. if (!type_check (mode, 1, BT_CHARACTER))
  1045. return false;
  1046. if (!kind_value_check (mode, 1, gfc_default_character_kind))
  1047. return false;
  1048. if (status == NULL)
  1049. return true;
  1050. if (!type_check (status, 2, BT_INTEGER))
  1051. return false;
  1052. if (!scalar_check (status, 2))
  1053. return false;
  1054. return true;
  1055. }
  1056. bool
  1057. gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
  1058. {
  1059. if (!numeric_check (x, 0))
  1060. return false;
  1061. if (y != NULL)
  1062. {
  1063. if (!numeric_check (y, 1))
  1064. return false;
  1065. if (x->ts.type == BT_COMPLEX)
  1066. {
  1067. gfc_error ("%qs argument of %qs intrinsic at %L must not be "
  1068. "present if %<x%> is COMPLEX",
  1069. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  1070. &y->where);
  1071. return false;
  1072. }
  1073. if (y->ts.type == BT_COMPLEX)
  1074. {
  1075. gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
  1076. "of either REAL or INTEGER",
  1077. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  1078. &y->where);
  1079. return false;
  1080. }
  1081. }
  1082. if (!kind_check (kind, 2, BT_COMPLEX))
  1083. return false;
  1084. if (!kind && warn_conversion
  1085. && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
  1086. gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
  1087. "COMPLEX(%d) at %L might lose precision, consider using "
  1088. "the KIND argument", gfc_typename (&x->ts),
  1089. gfc_default_real_kind, &x->where);
  1090. else if (y && !kind && warn_conversion
  1091. && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
  1092. gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
  1093. "COMPLEX(%d) at %L might lose precision, consider using "
  1094. "the KIND argument", gfc_typename (&y->ts),
  1095. gfc_default_real_kind, &y->where);
  1096. return true;
  1097. }
  1098. static bool
  1099. check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
  1100. gfc_expr *errmsg, bool co_reduce)
  1101. {
  1102. if (!variable_check (a, 0, false))
  1103. return false;
  1104. if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
  1105. "INTENT(INOUT)"))
  1106. return false;
  1107. /* Fortran 2008, 12.5.2.4, paragraph 18. */
  1108. if (gfc_has_vector_subscript (a))
  1109. {
  1110. gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
  1111. "subroutine %s shall not have a vector subscript",
  1112. &a->where, gfc_current_intrinsic);
  1113. return false;
  1114. }
  1115. if (gfc_is_coindexed (a))
  1116. {
  1117. gfc_error ("The A argument at %L to the intrinsic %s shall not be "
  1118. "coindexed", &a->where, gfc_current_intrinsic);
  1119. return false;
  1120. }
  1121. if (image_idx != NULL)
  1122. {
  1123. if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
  1124. return false;
  1125. if (!scalar_check (image_idx, co_reduce ? 2 : 1))
  1126. return false;
  1127. }
  1128. if (stat != NULL)
  1129. {
  1130. if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
  1131. return false;
  1132. if (!scalar_check (stat, co_reduce ? 3 : 2))
  1133. return false;
  1134. if (!variable_check (stat, co_reduce ? 3 : 2, false))
  1135. return false;
  1136. if (stat->ts.kind != 4)
  1137. {
  1138. gfc_error ("The stat= argument at %L must be a kind=4 integer "
  1139. "variable", &stat->where);
  1140. return false;
  1141. }
  1142. }
  1143. if (errmsg != NULL)
  1144. {
  1145. if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
  1146. return false;
  1147. if (!scalar_check (errmsg, co_reduce ? 4 : 3))
  1148. return false;
  1149. if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
  1150. return false;
  1151. if (errmsg->ts.kind != 1)
  1152. {
  1153. gfc_error ("The errmsg= argument at %L must be a default-kind "
  1154. "character variable", &errmsg->where);
  1155. return false;
  1156. }
  1157. }
  1158. if (flag_coarray == GFC_FCOARRAY_NONE)
  1159. {
  1160. gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
  1161. &a->where);
  1162. return false;
  1163. }
  1164. return true;
  1165. }
  1166. bool
  1167. gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
  1168. gfc_expr *errmsg)
  1169. {
  1170. if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
  1171. {
  1172. gfc_error ("Support for the A argument at %L which is polymorphic A "
  1173. "argument or has allocatable components is not yet "
  1174. "implemented", &a->where);
  1175. return false;
  1176. }
  1177. return check_co_collective (a, source_image, stat, errmsg, false);
  1178. }
  1179. bool
  1180. gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
  1181. gfc_expr *stat, gfc_expr *errmsg)
  1182. {
  1183. symbol_attribute attr;
  1184. gfc_formal_arglist *formal;
  1185. gfc_symbol *sym;
  1186. if (a->ts.type == BT_CLASS)
  1187. {
  1188. gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
  1189. &a->where);
  1190. return false;
  1191. }
  1192. if (gfc_expr_attr (a).alloc_comp)
  1193. {
  1194. gfc_error ("Support for the A argument at %L with allocatable components"
  1195. " is not yet implemented", &a->where);
  1196. return false;
  1197. }
  1198. if (!check_co_collective (a, result_image, stat, errmsg, true))
  1199. return false;
  1200. if (!gfc_resolve_expr (op))
  1201. return false;
  1202. attr = gfc_expr_attr (op);
  1203. if (!attr.pure || !attr.function)
  1204. {
  1205. gfc_error ("OPERATOR argument at %L must be a PURE function",
  1206. &op->where);
  1207. return false;
  1208. }
  1209. if (attr.intrinsic)
  1210. {
  1211. /* None of the intrinsics fulfills the criteria of taking two arguments,
  1212. returning the same type and kind as the arguments and being permitted
  1213. as actual argument. */
  1214. gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
  1215. op->symtree->n.sym->name, &op->where);
  1216. return false;
  1217. }
  1218. if (gfc_is_proc_ptr_comp (op))
  1219. {
  1220. gfc_component *comp = gfc_get_proc_ptr_comp (op);
  1221. sym = comp->ts.interface;
  1222. }
  1223. else
  1224. sym = op->symtree->n.sym;
  1225. formal = sym->formal;
  1226. if (!formal || !formal->next || formal->next->next)
  1227. {
  1228. gfc_error ("The function passed as OPERATOR at %L shall have two "
  1229. "arguments", &op->where);
  1230. return false;
  1231. }
  1232. if (sym->result->ts.type == BT_UNKNOWN)
  1233. gfc_set_default_type (sym->result, 0, NULL);
  1234. if (!gfc_compare_types (&a->ts, &sym->result->ts))
  1235. {
  1236. gfc_error_1 ("A argument at %L has type %s but the function passed as "
  1237. "OPERATOR at %L returns %s",
  1238. &a->where, gfc_typename (&a->ts), &op->where,
  1239. gfc_typename (&sym->result->ts));
  1240. return false;
  1241. }
  1242. if (!gfc_compare_types (&a->ts, &formal->sym->ts)
  1243. || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
  1244. {
  1245. gfc_error ("The function passed as OPERATOR at %L has arguments of type "
  1246. "%s and %s but shall have type %s", &op->where,
  1247. gfc_typename (&formal->sym->ts),
  1248. gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
  1249. return false;
  1250. }
  1251. if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
  1252. || formal->next->sym->as || formal->sym->attr.allocatable
  1253. || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
  1254. || formal->next->sym->attr.pointer)
  1255. {
  1256. gfc_error ("The function passed as OPERATOR at %L shall have scalar "
  1257. "nonallocatable nonpointer arguments and return a "
  1258. "nonallocatable nonpointer scalar", &op->where);
  1259. return false;
  1260. }
  1261. if (formal->sym->attr.value != formal->next->sym->attr.value)
  1262. {
  1263. gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
  1264. "attribute either for none or both arguments", &op->where);
  1265. return false;
  1266. }
  1267. if (formal->sym->attr.target != formal->next->sym->attr.target)
  1268. {
  1269. gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
  1270. "attribute either for none or both arguments", &op->where);
  1271. return false;
  1272. }
  1273. if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
  1274. {
  1275. gfc_error ("The function passed as OPERATOR at %L shall have the "
  1276. "ASYNCHRONOUS attribute either for none or both arguments",
  1277. &op->where);
  1278. return false;
  1279. }
  1280. if (formal->sym->attr.optional || formal->next->sym->attr.optional)
  1281. {
  1282. gfc_error ("The function passed as OPERATOR at %L shall not have the "
  1283. "OPTIONAL attribute for either of the arguments", &op->where);
  1284. return false;
  1285. }
  1286. if (a->ts.type == BT_CHARACTER)
  1287. {
  1288. gfc_charlen *cl;
  1289. unsigned long actual_size, formal_size1, formal_size2, result_size;
  1290. cl = a->ts.u.cl;
  1291. actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
  1292. ? mpz_get_ui (cl->length->value.integer) : 0;
  1293. cl = formal->sym->ts.u.cl;
  1294. formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
  1295. ? mpz_get_ui (cl->length->value.integer) : 0;
  1296. cl = formal->next->sym->ts.u.cl;
  1297. formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
  1298. ? mpz_get_ui (cl->length->value.integer) : 0;
  1299. cl = sym->ts.u.cl;
  1300. result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
  1301. ? mpz_get_ui (cl->length->value.integer) : 0;
  1302. if (actual_size
  1303. && ((formal_size1 && actual_size != formal_size1)
  1304. || (formal_size2 && actual_size != formal_size2)))
  1305. {
  1306. gfc_error_1 ("The character length of the A argument at %L and of the "
  1307. "arguments of the OPERATOR at %L shall be the same",
  1308. &a->where, &op->where);
  1309. return false;
  1310. }
  1311. if (actual_size && result_size && actual_size != result_size)
  1312. {
  1313. gfc_error_1 ("The character length of the A argument at %L and of the "
  1314. "function result of the OPERATOR at %L shall be the same",
  1315. &a->where, &op->where);
  1316. return false;
  1317. }
  1318. }
  1319. return true;
  1320. }
  1321. bool
  1322. gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
  1323. gfc_expr *errmsg)
  1324. {
  1325. if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
  1326. && a->ts.type != BT_CHARACTER)
  1327. {
  1328. gfc_error_1 ("'%s' argument of '%s' intrinsic at %L shall be of type "
  1329. "integer, real or character",
  1330. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  1331. &a->where);
  1332. return false;
  1333. }
  1334. return check_co_collective (a, result_image, stat, errmsg, false);
  1335. }
  1336. bool
  1337. gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
  1338. gfc_expr *errmsg)
  1339. {
  1340. if (!numeric_check (a, 0))
  1341. return false;
  1342. return check_co_collective (a, result_image, stat, errmsg, false);
  1343. }
  1344. bool
  1345. gfc_check_complex (gfc_expr *x, gfc_expr *y)
  1346. {
  1347. if (!int_or_real_check (x, 0))
  1348. return false;
  1349. if (!scalar_check (x, 0))
  1350. return false;
  1351. if (!int_or_real_check (y, 1))
  1352. return false;
  1353. if (!scalar_check (y, 1))
  1354. return false;
  1355. return true;
  1356. }
  1357. bool
  1358. gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
  1359. {
  1360. if (!logical_array_check (mask, 0))
  1361. return false;
  1362. if (!dim_check (dim, 1, false))
  1363. return false;
  1364. if (!dim_rank_check (dim, mask, 0))
  1365. return false;
  1366. if (!kind_check (kind, 2, BT_INTEGER))
  1367. return false;
  1368. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  1369. "with KIND argument at %L",
  1370. gfc_current_intrinsic, &kind->where))
  1371. return false;
  1372. return true;
  1373. }
  1374. bool
  1375. gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
  1376. {
  1377. if (!array_check (array, 0))
  1378. return false;
  1379. if (!type_check (shift, 1, BT_INTEGER))
  1380. return false;
  1381. if (!dim_check (dim, 2, true))
  1382. return false;
  1383. if (!dim_rank_check (dim, array, false))
  1384. return false;
  1385. if (array->rank == 1 || shift->rank == 0)
  1386. {
  1387. if (!scalar_check (shift, 1))
  1388. return false;
  1389. }
  1390. else if (shift->rank == array->rank - 1)
  1391. {
  1392. int d;
  1393. if (!dim)
  1394. d = 1;
  1395. else if (dim->expr_type == EXPR_CONSTANT)
  1396. gfc_extract_int (dim, &d);
  1397. else
  1398. d = -1;
  1399. if (d > 0)
  1400. {
  1401. int i, j;
  1402. for (i = 0, j = 0; i < array->rank; i++)
  1403. if (i != d - 1)
  1404. {
  1405. if (!identical_dimen_shape (array, i, shift, j))
  1406. {
  1407. gfc_error ("%qs argument of %qs intrinsic at %L has "
  1408. "invalid shape in dimension %d (%ld/%ld)",
  1409. gfc_current_intrinsic_arg[1]->name,
  1410. gfc_current_intrinsic, &shift->where, i + 1,
  1411. mpz_get_si (array->shape[i]),
  1412. mpz_get_si (shift->shape[j]));
  1413. return false;
  1414. }
  1415. j += 1;
  1416. }
  1417. }
  1418. }
  1419. else
  1420. {
  1421. gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
  1422. "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
  1423. gfc_current_intrinsic, &shift->where, array->rank - 1);
  1424. return false;
  1425. }
  1426. return true;
  1427. }
  1428. bool
  1429. gfc_check_ctime (gfc_expr *time)
  1430. {
  1431. if (!scalar_check (time, 0))
  1432. return false;
  1433. if (!type_check (time, 0, BT_INTEGER))
  1434. return false;
  1435. return true;
  1436. }
  1437. bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
  1438. {
  1439. if (!double_check (y, 0) || !double_check (x, 1))
  1440. return false;
  1441. return true;
  1442. }
  1443. bool
  1444. gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
  1445. {
  1446. if (!numeric_check (x, 0))
  1447. return false;
  1448. if (y != NULL)
  1449. {
  1450. if (!numeric_check (y, 1))
  1451. return false;
  1452. if (x->ts.type == BT_COMPLEX)
  1453. {
  1454. gfc_error ("%qs argument of %qs intrinsic at %L must not be "
  1455. "present if %<x%> is COMPLEX",
  1456. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  1457. &y->where);
  1458. return false;
  1459. }
  1460. if (y->ts.type == BT_COMPLEX)
  1461. {
  1462. gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
  1463. "of either REAL or INTEGER",
  1464. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  1465. &y->where);
  1466. return false;
  1467. }
  1468. }
  1469. return true;
  1470. }
  1471. bool
  1472. gfc_check_dble (gfc_expr *x)
  1473. {
  1474. if (!numeric_check (x, 0))
  1475. return false;
  1476. return true;
  1477. }
  1478. bool
  1479. gfc_check_digits (gfc_expr *x)
  1480. {
  1481. if (!int_or_real_check (x, 0))
  1482. return false;
  1483. return true;
  1484. }
  1485. bool
  1486. gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
  1487. {
  1488. switch (vector_a->ts.type)
  1489. {
  1490. case BT_LOGICAL:
  1491. if (!type_check (vector_b, 1, BT_LOGICAL))
  1492. return false;
  1493. break;
  1494. case BT_INTEGER:
  1495. case BT_REAL:
  1496. case BT_COMPLEX:
  1497. if (!numeric_check (vector_b, 1))
  1498. return false;
  1499. break;
  1500. default:
  1501. gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
  1502. "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
  1503. gfc_current_intrinsic, &vector_a->where);
  1504. return false;
  1505. }
  1506. if (!rank_check (vector_a, 0, 1))
  1507. return false;
  1508. if (!rank_check (vector_b, 1, 1))
  1509. return false;
  1510. if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
  1511. {
  1512. gfc_error ("Different shape for arguments %qs and %qs at %L for "
  1513. "intrinsic %<dot_product%>",
  1514. gfc_current_intrinsic_arg[0]->name,
  1515. gfc_current_intrinsic_arg[1]->name, &vector_a->where);
  1516. return false;
  1517. }
  1518. return true;
  1519. }
  1520. bool
  1521. gfc_check_dprod (gfc_expr *x, gfc_expr *y)
  1522. {
  1523. if (!type_check (x, 0, BT_REAL)
  1524. || !type_check (y, 1, BT_REAL))
  1525. return false;
  1526. if (x->ts.kind != gfc_default_real_kind)
  1527. {
  1528. gfc_error ("%qs argument of %qs intrinsic at %L must be default "
  1529. "real", gfc_current_intrinsic_arg[0]->name,
  1530. gfc_current_intrinsic, &x->where);
  1531. return false;
  1532. }
  1533. if (y->ts.kind != gfc_default_real_kind)
  1534. {
  1535. gfc_error ("%qs argument of %qs intrinsic at %L must be default "
  1536. "real", gfc_current_intrinsic_arg[1]->name,
  1537. gfc_current_intrinsic, &y->where);
  1538. return false;
  1539. }
  1540. return true;
  1541. }
  1542. bool
  1543. gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
  1544. {
  1545. if (!type_check (i, 0, BT_INTEGER))
  1546. return false;
  1547. if (!type_check (j, 1, BT_INTEGER))
  1548. return false;
  1549. if (i->is_boz && j->is_boz)
  1550. {
  1551. gfc_error_1 ("'I' at %L and 'J' at %L cannot both be BOZ literal "
  1552. "constants", &i->where, &j->where);
  1553. return false;
  1554. }
  1555. if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
  1556. return false;
  1557. if (!type_check (shift, 2, BT_INTEGER))
  1558. return false;
  1559. if (!nonnegative_check ("SHIFT", shift))
  1560. return false;
  1561. if (i->is_boz)
  1562. {
  1563. if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
  1564. return false;
  1565. i->ts.kind = j->ts.kind;
  1566. }
  1567. else
  1568. {
  1569. if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
  1570. return false;
  1571. j->ts.kind = i->ts.kind;
  1572. }
  1573. return true;
  1574. }
  1575. bool
  1576. gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
  1577. gfc_expr *dim)
  1578. {
  1579. if (!array_check (array, 0))
  1580. return false;
  1581. if (!type_check (shift, 1, BT_INTEGER))
  1582. return false;
  1583. if (!dim_check (dim, 3, true))
  1584. return false;
  1585. if (!dim_rank_check (dim, array, false))
  1586. return false;
  1587. if (array->rank == 1 || shift->rank == 0)
  1588. {
  1589. if (!scalar_check (shift, 1))
  1590. return false;
  1591. }
  1592. else if (shift->rank == array->rank - 1)
  1593. {
  1594. int d;
  1595. if (!dim)
  1596. d = 1;
  1597. else if (dim->expr_type == EXPR_CONSTANT)
  1598. gfc_extract_int (dim, &d);
  1599. else
  1600. d = -1;
  1601. if (d > 0)
  1602. {
  1603. int i, j;
  1604. for (i = 0, j = 0; i < array->rank; i++)
  1605. if (i != d - 1)
  1606. {
  1607. if (!identical_dimen_shape (array, i, shift, j))
  1608. {
  1609. gfc_error ("%qs argument of %qs intrinsic at %L has "
  1610. "invalid shape in dimension %d (%ld/%ld)",
  1611. gfc_current_intrinsic_arg[1]->name,
  1612. gfc_current_intrinsic, &shift->where, i + 1,
  1613. mpz_get_si (array->shape[i]),
  1614. mpz_get_si (shift->shape[j]));
  1615. return false;
  1616. }
  1617. j += 1;
  1618. }
  1619. }
  1620. }
  1621. else
  1622. {
  1623. gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
  1624. "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
  1625. gfc_current_intrinsic, &shift->where, array->rank - 1);
  1626. return false;
  1627. }
  1628. if (boundary != NULL)
  1629. {
  1630. if (!same_type_check (array, 0, boundary, 2))
  1631. return false;
  1632. if (array->rank == 1 || boundary->rank == 0)
  1633. {
  1634. if (!scalar_check (boundary, 2))
  1635. return false;
  1636. }
  1637. else if (boundary->rank == array->rank - 1)
  1638. {
  1639. if (!gfc_check_conformance (shift, boundary,
  1640. "arguments '%s' and '%s' for "
  1641. "intrinsic %s",
  1642. gfc_current_intrinsic_arg[1]->name,
  1643. gfc_current_intrinsic_arg[2]->name,
  1644. gfc_current_intrinsic))
  1645. return false;
  1646. }
  1647. else
  1648. {
  1649. gfc_error ("%qs argument of intrinsic %qs at %L of must have "
  1650. "rank %d or be a scalar",
  1651. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  1652. &shift->where, array->rank - 1);
  1653. return false;
  1654. }
  1655. }
  1656. return true;
  1657. }
  1658. bool
  1659. gfc_check_float (gfc_expr *a)
  1660. {
  1661. if (!type_check (a, 0, BT_INTEGER))
  1662. return false;
  1663. if ((a->ts.kind != gfc_default_integer_kind)
  1664. && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
  1665. "kind argument to %s intrinsic at %L",
  1666. gfc_current_intrinsic, &a->where))
  1667. return false;
  1668. return true;
  1669. }
  1670. /* A single complex argument. */
  1671. bool
  1672. gfc_check_fn_c (gfc_expr *a)
  1673. {
  1674. if (!type_check (a, 0, BT_COMPLEX))
  1675. return false;
  1676. return true;
  1677. }
  1678. /* A single real argument. */
  1679. bool
  1680. gfc_check_fn_r (gfc_expr *a)
  1681. {
  1682. if (!type_check (a, 0, BT_REAL))
  1683. return false;
  1684. return true;
  1685. }
  1686. /* A single double argument. */
  1687. bool
  1688. gfc_check_fn_d (gfc_expr *a)
  1689. {
  1690. if (!double_check (a, 0))
  1691. return false;
  1692. return true;
  1693. }
  1694. /* A single real or complex argument. */
  1695. bool
  1696. gfc_check_fn_rc (gfc_expr *a)
  1697. {
  1698. if (!real_or_complex_check (a, 0))
  1699. return false;
  1700. return true;
  1701. }
  1702. bool
  1703. gfc_check_fn_rc2008 (gfc_expr *a)
  1704. {
  1705. if (!real_or_complex_check (a, 0))
  1706. return false;
  1707. if (a->ts.type == BT_COMPLEX
  1708. && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
  1709. "of %qs intrinsic at %L",
  1710. gfc_current_intrinsic_arg[0]->name,
  1711. gfc_current_intrinsic, &a->where))
  1712. return false;
  1713. return true;
  1714. }
  1715. bool
  1716. gfc_check_fnum (gfc_expr *unit)
  1717. {
  1718. if (!type_check (unit, 0, BT_INTEGER))
  1719. return false;
  1720. if (!scalar_check (unit, 0))
  1721. return false;
  1722. return true;
  1723. }
  1724. bool
  1725. gfc_check_huge (gfc_expr *x)
  1726. {
  1727. if (!int_or_real_check (x, 0))
  1728. return false;
  1729. return true;
  1730. }
  1731. bool
  1732. gfc_check_hypot (gfc_expr *x, gfc_expr *y)
  1733. {
  1734. if (!type_check (x, 0, BT_REAL))
  1735. return false;
  1736. if (!same_type_check (x, 0, y, 1))
  1737. return false;
  1738. return true;
  1739. }
  1740. /* Check that the single argument is an integer. */
  1741. bool
  1742. gfc_check_i (gfc_expr *i)
  1743. {
  1744. if (!type_check (i, 0, BT_INTEGER))
  1745. return false;
  1746. return true;
  1747. }
  1748. bool
  1749. gfc_check_iand (gfc_expr *i, gfc_expr *j)
  1750. {
  1751. if (!type_check (i, 0, BT_INTEGER))
  1752. return false;
  1753. if (!type_check (j, 1, BT_INTEGER))
  1754. return false;
  1755. if (i->ts.kind != j->ts.kind)
  1756. {
  1757. if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
  1758. &i->where))
  1759. return false;
  1760. }
  1761. return true;
  1762. }
  1763. bool
  1764. gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
  1765. {
  1766. if (!type_check (i, 0, BT_INTEGER))
  1767. return false;
  1768. if (!type_check (pos, 1, BT_INTEGER))
  1769. return false;
  1770. if (!type_check (len, 2, BT_INTEGER))
  1771. return false;
  1772. if (!nonnegative_check ("pos", pos))
  1773. return false;
  1774. if (!nonnegative_check ("len", len))
  1775. return false;
  1776. if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
  1777. return false;
  1778. return true;
  1779. }
  1780. bool
  1781. gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
  1782. {
  1783. int i;
  1784. if (!type_check (c, 0, BT_CHARACTER))
  1785. return false;
  1786. if (!kind_check (kind, 1, BT_INTEGER))
  1787. return false;
  1788. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  1789. "with KIND argument at %L",
  1790. gfc_current_intrinsic, &kind->where))
  1791. return false;
  1792. if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
  1793. {
  1794. gfc_expr *start;
  1795. gfc_expr *end;
  1796. gfc_ref *ref;
  1797. /* Substring references don't have the charlength set. */
  1798. ref = c->ref;
  1799. while (ref && ref->type != REF_SUBSTRING)
  1800. ref = ref->next;
  1801. gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
  1802. if (!ref)
  1803. {
  1804. /* Check that the argument is length one. Non-constant lengths
  1805. can't be checked here, so assume they are ok. */
  1806. if (c->ts.u.cl && c->ts.u.cl->length)
  1807. {
  1808. /* If we already have a length for this expression then use it. */
  1809. if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
  1810. return true;
  1811. i = mpz_get_si (c->ts.u.cl->length->value.integer);
  1812. }
  1813. else
  1814. return true;
  1815. }
  1816. else
  1817. {
  1818. start = ref->u.ss.start;
  1819. end = ref->u.ss.end;
  1820. gcc_assert (start);
  1821. if (end == NULL || end->expr_type != EXPR_CONSTANT
  1822. || start->expr_type != EXPR_CONSTANT)
  1823. return true;
  1824. i = mpz_get_si (end->value.integer) + 1
  1825. - mpz_get_si (start->value.integer);
  1826. }
  1827. }
  1828. else
  1829. return true;
  1830. if (i != 1)
  1831. {
  1832. gfc_error ("Argument of %s at %L must be of length one",
  1833. gfc_current_intrinsic, &c->where);
  1834. return false;
  1835. }
  1836. return true;
  1837. }
  1838. bool
  1839. gfc_check_idnint (gfc_expr *a)
  1840. {
  1841. if (!double_check (a, 0))
  1842. return false;
  1843. return true;
  1844. }
  1845. bool
  1846. gfc_check_ieor (gfc_expr *i, gfc_expr *j)
  1847. {
  1848. if (!type_check (i, 0, BT_INTEGER))
  1849. return false;
  1850. if (!type_check (j, 1, BT_INTEGER))
  1851. return false;
  1852. if (i->ts.kind != j->ts.kind)
  1853. {
  1854. if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
  1855. &i->where))
  1856. return false;
  1857. }
  1858. return true;
  1859. }
  1860. bool
  1861. gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
  1862. gfc_expr *kind)
  1863. {
  1864. if (!type_check (string, 0, BT_CHARACTER)
  1865. || !type_check (substring, 1, BT_CHARACTER))
  1866. return false;
  1867. if (back != NULL && !type_check (back, 2, BT_LOGICAL))
  1868. return false;
  1869. if (!kind_check (kind, 3, BT_INTEGER))
  1870. return false;
  1871. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  1872. "with KIND argument at %L",
  1873. gfc_current_intrinsic, &kind->where))
  1874. return false;
  1875. if (string->ts.kind != substring->ts.kind)
  1876. {
  1877. gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
  1878. "kind as %qs", gfc_current_intrinsic_arg[1]->name,
  1879. gfc_current_intrinsic, &substring->where,
  1880. gfc_current_intrinsic_arg[0]->name);
  1881. return false;
  1882. }
  1883. return true;
  1884. }
  1885. bool
  1886. gfc_check_int (gfc_expr *x, gfc_expr *kind)
  1887. {
  1888. if (!numeric_check (x, 0))
  1889. return false;
  1890. if (!kind_check (kind, 1, BT_INTEGER))
  1891. return false;
  1892. return true;
  1893. }
  1894. bool
  1895. gfc_check_intconv (gfc_expr *x)
  1896. {
  1897. if (!numeric_check (x, 0))
  1898. return false;
  1899. return true;
  1900. }
  1901. bool
  1902. gfc_check_ior (gfc_expr *i, gfc_expr *j)
  1903. {
  1904. if (!type_check (i, 0, BT_INTEGER))
  1905. return false;
  1906. if (!type_check (j, 1, BT_INTEGER))
  1907. return false;
  1908. if (i->ts.kind != j->ts.kind)
  1909. {
  1910. if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
  1911. &i->where))
  1912. return false;
  1913. }
  1914. return true;
  1915. }
  1916. bool
  1917. gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
  1918. {
  1919. if (!type_check (i, 0, BT_INTEGER)
  1920. || !type_check (shift, 1, BT_INTEGER))
  1921. return false;
  1922. if (!less_than_bitsize1 ("I", i, NULL, shift, true))
  1923. return false;
  1924. return true;
  1925. }
  1926. bool
  1927. gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
  1928. {
  1929. if (!type_check (i, 0, BT_INTEGER)
  1930. || !type_check (shift, 1, BT_INTEGER))
  1931. return false;
  1932. if (size != NULL)
  1933. {
  1934. int i2, i3;
  1935. if (!type_check (size, 2, BT_INTEGER))
  1936. return false;
  1937. if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
  1938. return false;
  1939. if (size->expr_type == EXPR_CONSTANT)
  1940. {
  1941. gfc_extract_int (size, &i3);
  1942. if (i3 <= 0)
  1943. {
  1944. gfc_error ("SIZE at %L must be positive", &size->where);
  1945. return false;
  1946. }
  1947. if (shift->expr_type == EXPR_CONSTANT)
  1948. {
  1949. gfc_extract_int (shift, &i2);
  1950. if (i2 < 0)
  1951. i2 = -i2;
  1952. if (i2 > i3)
  1953. {
  1954. gfc_error_1 ("The absolute value of SHIFT at %L must be less "
  1955. "than or equal to SIZE at %L", &shift->where,
  1956. &size->where);
  1957. return false;
  1958. }
  1959. }
  1960. }
  1961. }
  1962. else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
  1963. return false;
  1964. return true;
  1965. }
  1966. bool
  1967. gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
  1968. {
  1969. if (!type_check (pid, 0, BT_INTEGER))
  1970. return false;
  1971. if (!type_check (sig, 1, BT_INTEGER))
  1972. return false;
  1973. return true;
  1974. }
  1975. bool
  1976. gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
  1977. {
  1978. if (!type_check (pid, 0, BT_INTEGER))
  1979. return false;
  1980. if (!scalar_check (pid, 0))
  1981. return false;
  1982. if (!type_check (sig, 1, BT_INTEGER))
  1983. return false;
  1984. if (!scalar_check (sig, 1))
  1985. return false;
  1986. if (status == NULL)
  1987. return true;
  1988. if (!type_check (status, 2, BT_INTEGER))
  1989. return false;
  1990. if (!scalar_check (status, 2))
  1991. return false;
  1992. return true;
  1993. }
  1994. bool
  1995. gfc_check_kind (gfc_expr *x)
  1996. {
  1997. if (x->ts.type == BT_DERIVED || x->ts.type == BT_CLASS)
  1998. {
  1999. gfc_error ("%qs argument of %qs intrinsic at %L must be of "
  2000. "intrinsic type", gfc_current_intrinsic_arg[0]->name,
  2001. gfc_current_intrinsic, &x->where);
  2002. return false;
  2003. }
  2004. if (x->ts.type == BT_PROCEDURE)
  2005. {
  2006. gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
  2007. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  2008. &x->where);
  2009. return false;
  2010. }
  2011. return true;
  2012. }
  2013. bool
  2014. gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
  2015. {
  2016. if (!array_check (array, 0))
  2017. return false;
  2018. if (!dim_check (dim, 1, false))
  2019. return false;
  2020. if (!dim_rank_check (dim, array, 1))
  2021. return false;
  2022. if (!kind_check (kind, 2, BT_INTEGER))
  2023. return false;
  2024. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  2025. "with KIND argument at %L",
  2026. gfc_current_intrinsic, &kind->where))
  2027. return false;
  2028. return true;
  2029. }
  2030. bool
  2031. gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
  2032. {
  2033. if (flag_coarray == GFC_FCOARRAY_NONE)
  2034. {
  2035. gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
  2036. return false;
  2037. }
  2038. if (!coarray_check (coarray, 0))
  2039. return false;
  2040. if (dim != NULL)
  2041. {
  2042. if (!dim_check (dim, 1, false))
  2043. return false;
  2044. if (!dim_corank_check (dim, coarray))
  2045. return false;
  2046. }
  2047. if (!kind_check (kind, 2, BT_INTEGER))
  2048. return false;
  2049. return true;
  2050. }
  2051. bool
  2052. gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
  2053. {
  2054. if (!type_check (s, 0, BT_CHARACTER))
  2055. return false;
  2056. if (!kind_check (kind, 1, BT_INTEGER))
  2057. return false;
  2058. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  2059. "with KIND argument at %L",
  2060. gfc_current_intrinsic, &kind->where))
  2061. return false;
  2062. return true;
  2063. }
  2064. bool
  2065. gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
  2066. {
  2067. if (!type_check (a, 0, BT_CHARACTER))
  2068. return false;
  2069. if (!kind_value_check (a, 0, gfc_default_character_kind))
  2070. return false;
  2071. if (!type_check (b, 1, BT_CHARACTER))
  2072. return false;
  2073. if (!kind_value_check (b, 1, gfc_default_character_kind))
  2074. return false;
  2075. return true;
  2076. }
  2077. bool
  2078. gfc_check_link (gfc_expr *path1, gfc_expr *path2)
  2079. {
  2080. if (!type_check (path1, 0, BT_CHARACTER))
  2081. return false;
  2082. if (!kind_value_check (path1, 0, gfc_default_character_kind))
  2083. return false;
  2084. if (!type_check (path2, 1, BT_CHARACTER))
  2085. return false;
  2086. if (!kind_value_check (path2, 1, gfc_default_character_kind))
  2087. return false;
  2088. return true;
  2089. }
  2090. bool
  2091. gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
  2092. {
  2093. if (!type_check (path1, 0, BT_CHARACTER))
  2094. return false;
  2095. if (!kind_value_check (path1, 0, gfc_default_character_kind))
  2096. return false;
  2097. if (!type_check (path2, 1, BT_CHARACTER))
  2098. return false;
  2099. if (!kind_value_check (path2, 0, gfc_default_character_kind))
  2100. return false;
  2101. if (status == NULL)
  2102. return true;
  2103. if (!type_check (status, 2, BT_INTEGER))
  2104. return false;
  2105. if (!scalar_check (status, 2))
  2106. return false;
  2107. return true;
  2108. }
  2109. bool
  2110. gfc_check_loc (gfc_expr *expr)
  2111. {
  2112. return variable_check (expr, 0, true);
  2113. }
  2114. bool
  2115. gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
  2116. {
  2117. if (!type_check (path1, 0, BT_CHARACTER))
  2118. return false;
  2119. if (!kind_value_check (path1, 0, gfc_default_character_kind))
  2120. return false;
  2121. if (!type_check (path2, 1, BT_CHARACTER))
  2122. return false;
  2123. if (!kind_value_check (path2, 1, gfc_default_character_kind))
  2124. return false;
  2125. return true;
  2126. }
  2127. bool
  2128. gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
  2129. {
  2130. if (!type_check (path1, 0, BT_CHARACTER))
  2131. return false;
  2132. if (!kind_value_check (path1, 0, gfc_default_character_kind))
  2133. return false;
  2134. if (!type_check (path2, 1, BT_CHARACTER))
  2135. return false;
  2136. if (!kind_value_check (path2, 1, gfc_default_character_kind))
  2137. return false;
  2138. if (status == NULL)
  2139. return true;
  2140. if (!type_check (status, 2, BT_INTEGER))
  2141. return false;
  2142. if (!scalar_check (status, 2))
  2143. return false;
  2144. return true;
  2145. }
  2146. bool
  2147. gfc_check_logical (gfc_expr *a, gfc_expr *kind)
  2148. {
  2149. if (!type_check (a, 0, BT_LOGICAL))
  2150. return false;
  2151. if (!kind_check (kind, 1, BT_LOGICAL))
  2152. return false;
  2153. return true;
  2154. }
  2155. /* Min/max family. */
  2156. static bool
  2157. min_max_args (gfc_actual_arglist *args)
  2158. {
  2159. gfc_actual_arglist *arg;
  2160. int i, j, nargs, *nlabels, nlabelless;
  2161. bool a1 = false, a2 = false;
  2162. if (args == NULL || args->next == NULL)
  2163. {
  2164. gfc_error ("Intrinsic %qs at %L must have at least two arguments",
  2165. gfc_current_intrinsic, gfc_current_intrinsic_where);
  2166. return false;
  2167. }
  2168. if (!args->name)
  2169. a1 = true;
  2170. if (!args->next->name)
  2171. a2 = true;
  2172. nargs = 0;
  2173. for (arg = args; arg; arg = arg->next)
  2174. if (arg->name)
  2175. nargs++;
  2176. if (nargs == 0)
  2177. return true;
  2178. /* Note: Having a keywordless argument after an "arg=" is checked before. */
  2179. nlabelless = 0;
  2180. nlabels = XALLOCAVEC (int, nargs);
  2181. for (arg = args, i = 0; arg; arg = arg->next, i++)
  2182. if (arg->name)
  2183. {
  2184. int n;
  2185. char *endp;
  2186. if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
  2187. goto unknown;
  2188. n = strtol (&arg->name[1], &endp, 10);
  2189. if (endp[0] != '\0')
  2190. goto unknown;
  2191. if (n <= 0)
  2192. goto unknown;
  2193. if (n <= nlabelless)
  2194. goto duplicate;
  2195. nlabels[i] = n;
  2196. if (n == 1)
  2197. a1 = true;
  2198. if (n == 2)
  2199. a2 = true;
  2200. }
  2201. else
  2202. nlabelless++;
  2203. if (!a1 || !a2)
  2204. {
  2205. gfc_error ("Missing %qs argument to the %s intrinsic at %L",
  2206. !a1 ? "a1" : "a2", gfc_current_intrinsic,
  2207. gfc_current_intrinsic_where);
  2208. return false;
  2209. }
  2210. /* Check for duplicates. */
  2211. for (i = 0; i < nargs; i++)
  2212. for (j = i + 1; j < nargs; j++)
  2213. if (nlabels[i] == nlabels[j])
  2214. goto duplicate;
  2215. return true;
  2216. duplicate:
  2217. gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
  2218. &arg->expr->where, gfc_current_intrinsic);
  2219. return false;
  2220. unknown:
  2221. gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
  2222. &arg->expr->where, gfc_current_intrinsic);
  2223. return false;
  2224. }
  2225. static bool
  2226. check_rest (bt type, int kind, gfc_actual_arglist *arglist)
  2227. {
  2228. gfc_actual_arglist *arg, *tmp;
  2229. gfc_expr *x;
  2230. int m, n;
  2231. if (!min_max_args (arglist))
  2232. return false;
  2233. for (arg = arglist, n=1; arg; arg = arg->next, n++)
  2234. {
  2235. x = arg->expr;
  2236. if (x->ts.type != type || x->ts.kind != kind)
  2237. {
  2238. if (x->ts.type == type)
  2239. {
  2240. if (!gfc_notify_std (GFC_STD_GNU, "Different type "
  2241. "kinds at %L", &x->where))
  2242. return false;
  2243. }
  2244. else
  2245. {
  2246. gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
  2247. "%s(%d)", n, gfc_current_intrinsic, &x->where,
  2248. gfc_basic_typename (type), kind);
  2249. return false;
  2250. }
  2251. }
  2252. for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
  2253. if (!gfc_check_conformance (tmp->expr, x,
  2254. "arguments 'a%d' and 'a%d' for "
  2255. "intrinsic '%s'", m, n,
  2256. gfc_current_intrinsic))
  2257. return false;
  2258. }
  2259. return true;
  2260. }
  2261. bool
  2262. gfc_check_min_max (gfc_actual_arglist *arg)
  2263. {
  2264. gfc_expr *x;
  2265. if (!min_max_args (arg))
  2266. return false;
  2267. x = arg->expr;
  2268. if (x->ts.type == BT_CHARACTER)
  2269. {
  2270. if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  2271. "with CHARACTER argument at %L",
  2272. gfc_current_intrinsic, &x->where))
  2273. return false;
  2274. }
  2275. else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
  2276. {
  2277. gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
  2278. "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
  2279. return false;
  2280. }
  2281. return check_rest (x->ts.type, x->ts.kind, arg);
  2282. }
  2283. bool
  2284. gfc_check_min_max_integer (gfc_actual_arglist *arg)
  2285. {
  2286. return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
  2287. }
  2288. bool
  2289. gfc_check_min_max_real (gfc_actual_arglist *arg)
  2290. {
  2291. return check_rest (BT_REAL, gfc_default_real_kind, arg);
  2292. }
  2293. bool
  2294. gfc_check_min_max_double (gfc_actual_arglist *arg)
  2295. {
  2296. return check_rest (BT_REAL, gfc_default_double_kind, arg);
  2297. }
  2298. /* End of min/max family. */
  2299. bool
  2300. gfc_check_malloc (gfc_expr *size)
  2301. {
  2302. if (!type_check (size, 0, BT_INTEGER))
  2303. return false;
  2304. if (!scalar_check (size, 0))
  2305. return false;
  2306. return true;
  2307. }
  2308. bool
  2309. gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
  2310. {
  2311. if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
  2312. {
  2313. gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
  2314. "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
  2315. gfc_current_intrinsic, &matrix_a->where);
  2316. return false;
  2317. }
  2318. if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
  2319. {
  2320. gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
  2321. "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
  2322. gfc_current_intrinsic, &matrix_b->where);
  2323. return false;
  2324. }
  2325. if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
  2326. || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
  2327. {
  2328. gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
  2329. gfc_current_intrinsic, &matrix_a->where,
  2330. gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
  2331. return false;
  2332. }
  2333. switch (matrix_a->rank)
  2334. {
  2335. case 1:
  2336. if (!rank_check (matrix_b, 1, 2))
  2337. return false;
  2338. /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
  2339. if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
  2340. {
  2341. gfc_error ("Different shape on dimension 1 for arguments %qs "
  2342. "and %qs at %L for intrinsic matmul",
  2343. gfc_current_intrinsic_arg[0]->name,
  2344. gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
  2345. return false;
  2346. }
  2347. break;
  2348. case 2:
  2349. if (matrix_b->rank != 2)
  2350. {
  2351. if (!rank_check (matrix_b, 1, 1))
  2352. return false;
  2353. }
  2354. /* matrix_b has rank 1 or 2 here. Common check for the cases
  2355. - matrix_a has shape (n,m) and matrix_b has shape (m, k)
  2356. - matrix_a has shape (n,m) and matrix_b has shape (m). */
  2357. if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
  2358. {
  2359. gfc_error ("Different shape on dimension 2 for argument %qs and "
  2360. "dimension 1 for argument %qs at %L for intrinsic "
  2361. "matmul", gfc_current_intrinsic_arg[0]->name,
  2362. gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
  2363. return false;
  2364. }
  2365. break;
  2366. default:
  2367. gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
  2368. "1 or 2", gfc_current_intrinsic_arg[0]->name,
  2369. gfc_current_intrinsic, &matrix_a->where);
  2370. return false;
  2371. }
  2372. return true;
  2373. }
  2374. /* Whoever came up with this interface was probably on something.
  2375. The possibilities for the occupation of the second and third
  2376. parameters are:
  2377. Arg #2 Arg #3
  2378. NULL NULL
  2379. DIM NULL
  2380. MASK NULL
  2381. NULL MASK minloc(array, mask=m)
  2382. DIM MASK
  2383. I.e. in the case of minloc(array,mask), mask will be in the second
  2384. position of the argument list and we'll have to fix that up. */
  2385. bool
  2386. gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
  2387. {
  2388. gfc_expr *a, *m, *d;
  2389. a = ap->expr;
  2390. if (!int_or_real_check (a, 0) || !array_check (a, 0))
  2391. return false;
  2392. d = ap->next->expr;
  2393. m = ap->next->next->expr;
  2394. if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
  2395. && ap->next->name == NULL)
  2396. {
  2397. m = d;
  2398. d = NULL;
  2399. ap->next->expr = NULL;
  2400. ap->next->next->expr = m;
  2401. }
  2402. if (!dim_check (d, 1, false))
  2403. return false;
  2404. if (!dim_rank_check (d, a, 0))
  2405. return false;
  2406. if (m != NULL && !type_check (m, 2, BT_LOGICAL))
  2407. return false;
  2408. if (m != NULL
  2409. && !gfc_check_conformance (a, m,
  2410. "arguments '%s' and '%s' for intrinsic %s",
  2411. gfc_current_intrinsic_arg[0]->name,
  2412. gfc_current_intrinsic_arg[2]->name,
  2413. gfc_current_intrinsic))
  2414. return false;
  2415. return true;
  2416. }
  2417. /* Similar to minloc/maxloc, the argument list might need to be
  2418. reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
  2419. difference is that MINLOC/MAXLOC take an additional KIND argument.
  2420. The possibilities are:
  2421. Arg #2 Arg #3
  2422. NULL NULL
  2423. DIM NULL
  2424. MASK NULL
  2425. NULL MASK minval(array, mask=m)
  2426. DIM MASK
  2427. I.e. in the case of minval(array,mask), mask will be in the second
  2428. position of the argument list and we'll have to fix that up. */
  2429. static bool
  2430. check_reduction (gfc_actual_arglist *ap)
  2431. {
  2432. gfc_expr *a, *m, *d;
  2433. a = ap->expr;
  2434. d = ap->next->expr;
  2435. m = ap->next->next->expr;
  2436. if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
  2437. && ap->next->name == NULL)
  2438. {
  2439. m = d;
  2440. d = NULL;
  2441. ap->next->expr = NULL;
  2442. ap->next->next->expr = m;
  2443. }
  2444. if (!dim_check (d, 1, false))
  2445. return false;
  2446. if (!dim_rank_check (d, a, 0))
  2447. return false;
  2448. if (m != NULL && !type_check (m, 2, BT_LOGICAL))
  2449. return false;
  2450. if (m != NULL
  2451. && !gfc_check_conformance (a, m,
  2452. "arguments '%s' and '%s' for intrinsic %s",
  2453. gfc_current_intrinsic_arg[0]->name,
  2454. gfc_current_intrinsic_arg[2]->name,
  2455. gfc_current_intrinsic))
  2456. return false;
  2457. return true;
  2458. }
  2459. bool
  2460. gfc_check_minval_maxval (gfc_actual_arglist *ap)
  2461. {
  2462. if (!int_or_real_check (ap->expr, 0)
  2463. || !array_check (ap->expr, 0))
  2464. return false;
  2465. return check_reduction (ap);
  2466. }
  2467. bool
  2468. gfc_check_product_sum (gfc_actual_arglist *ap)
  2469. {
  2470. if (!numeric_check (ap->expr, 0)
  2471. || !array_check (ap->expr, 0))
  2472. return false;
  2473. return check_reduction (ap);
  2474. }
  2475. /* For IANY, IALL and IPARITY. */
  2476. bool
  2477. gfc_check_mask (gfc_expr *i, gfc_expr *kind)
  2478. {
  2479. int k;
  2480. if (!type_check (i, 0, BT_INTEGER))
  2481. return false;
  2482. if (!nonnegative_check ("I", i))
  2483. return false;
  2484. if (!kind_check (kind, 1, BT_INTEGER))
  2485. return false;
  2486. if (kind)
  2487. gfc_extract_int (kind, &k);
  2488. else
  2489. k = gfc_default_integer_kind;
  2490. if (!less_than_bitsizekind ("I", i, k))
  2491. return false;
  2492. return true;
  2493. }
  2494. bool
  2495. gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
  2496. {
  2497. if (ap->expr->ts.type != BT_INTEGER)
  2498. {
  2499. gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
  2500. gfc_current_intrinsic_arg[0]->name,
  2501. gfc_current_intrinsic, &ap->expr->where);
  2502. return false;
  2503. }
  2504. if (!array_check (ap->expr, 0))
  2505. return false;
  2506. return check_reduction (ap);
  2507. }
  2508. bool
  2509. gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
  2510. {
  2511. if (!same_type_check (tsource, 0, fsource, 1))
  2512. return false;
  2513. if (!type_check (mask, 2, BT_LOGICAL))
  2514. return false;
  2515. if (tsource->ts.type == BT_CHARACTER)
  2516. return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
  2517. return true;
  2518. }
  2519. bool
  2520. gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
  2521. {
  2522. if (!type_check (i, 0, BT_INTEGER))
  2523. return false;
  2524. if (!type_check (j, 1, BT_INTEGER))
  2525. return false;
  2526. if (!type_check (mask, 2, BT_INTEGER))
  2527. return false;
  2528. if (!same_type_check (i, 0, j, 1))
  2529. return false;
  2530. if (!same_type_check (i, 0, mask, 2))
  2531. return false;
  2532. return true;
  2533. }
  2534. bool
  2535. gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
  2536. {
  2537. if (!variable_check (from, 0, false))
  2538. return false;
  2539. if (!allocatable_check (from, 0))
  2540. return false;
  2541. if (gfc_is_coindexed (from))
  2542. {
  2543. gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
  2544. "coindexed", &from->where);
  2545. return false;
  2546. }
  2547. if (!variable_check (to, 1, false))
  2548. return false;
  2549. if (!allocatable_check (to, 1))
  2550. return false;
  2551. if (gfc_is_coindexed (to))
  2552. {
  2553. gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
  2554. "coindexed", &to->where);
  2555. return false;
  2556. }
  2557. if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
  2558. {
  2559. gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
  2560. "polymorphic if FROM is polymorphic",
  2561. &to->where);
  2562. return false;
  2563. }
  2564. if (!same_type_check (to, 1, from, 0))
  2565. return false;
  2566. if (to->rank != from->rank)
  2567. {
  2568. gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
  2569. "must have the same rank %d/%d", &to->where, from->rank,
  2570. to->rank);
  2571. return false;
  2572. }
  2573. /* IR F08/0040; cf. 12-006A. */
  2574. if (gfc_get_corank (to) != gfc_get_corank (from))
  2575. {
  2576. gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
  2577. "must have the same corank %d/%d", &to->where,
  2578. gfc_get_corank (from), gfc_get_corank (to));
  2579. return false;
  2580. }
  2581. /* CLASS arguments: Make sure the vtab of from is present. */
  2582. if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
  2583. gfc_find_vtab (&from->ts);
  2584. return true;
  2585. }
  2586. bool
  2587. gfc_check_nearest (gfc_expr *x, gfc_expr *s)
  2588. {
  2589. if (!type_check (x, 0, BT_REAL))
  2590. return false;
  2591. if (!type_check (s, 1, BT_REAL))
  2592. return false;
  2593. if (s->expr_type == EXPR_CONSTANT)
  2594. {
  2595. if (mpfr_sgn (s->value.real) == 0)
  2596. {
  2597. gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
  2598. &s->where);
  2599. return false;
  2600. }
  2601. }
  2602. return true;
  2603. }
  2604. bool
  2605. gfc_check_new_line (gfc_expr *a)
  2606. {
  2607. if (!type_check (a, 0, BT_CHARACTER))
  2608. return false;
  2609. return true;
  2610. }
  2611. bool
  2612. gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
  2613. {
  2614. if (!type_check (array, 0, BT_REAL))
  2615. return false;
  2616. if (!array_check (array, 0))
  2617. return false;
  2618. if (!dim_rank_check (dim, array, false))
  2619. return false;
  2620. return true;
  2621. }
  2622. bool
  2623. gfc_check_null (gfc_expr *mold)
  2624. {
  2625. symbol_attribute attr;
  2626. if (mold == NULL)
  2627. return true;
  2628. if (!variable_check (mold, 0, true))
  2629. return false;
  2630. attr = gfc_variable_attr (mold, NULL);
  2631. if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
  2632. {
  2633. gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
  2634. "ALLOCATABLE or procedure pointer",
  2635. gfc_current_intrinsic_arg[0]->name,
  2636. gfc_current_intrinsic, &mold->where);
  2637. return false;
  2638. }
  2639. if (attr.allocatable
  2640. && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
  2641. "allocatable MOLD at %L", &mold->where))
  2642. return false;
  2643. /* F2008, C1242. */
  2644. if (gfc_is_coindexed (mold))
  2645. {
  2646. gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
  2647. "coindexed", gfc_current_intrinsic_arg[0]->name,
  2648. gfc_current_intrinsic, &mold->where);
  2649. return false;
  2650. }
  2651. return true;
  2652. }
  2653. bool
  2654. gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
  2655. {
  2656. if (!array_check (array, 0))
  2657. return false;
  2658. if (!type_check (mask, 1, BT_LOGICAL))
  2659. return false;
  2660. if (!gfc_check_conformance (array, mask,
  2661. "arguments '%s' and '%s' for intrinsic '%s'",
  2662. gfc_current_intrinsic_arg[0]->name,
  2663. gfc_current_intrinsic_arg[1]->name,
  2664. gfc_current_intrinsic))
  2665. return false;
  2666. if (vector != NULL)
  2667. {
  2668. mpz_t array_size, vector_size;
  2669. bool have_array_size, have_vector_size;
  2670. if (!same_type_check (array, 0, vector, 2))
  2671. return false;
  2672. if (!rank_check (vector, 2, 1))
  2673. return false;
  2674. /* VECTOR requires at least as many elements as MASK
  2675. has .TRUE. values. */
  2676. have_array_size = gfc_array_size(array, &array_size);
  2677. have_vector_size = gfc_array_size(vector, &vector_size);
  2678. if (have_vector_size
  2679. && (mask->expr_type == EXPR_ARRAY
  2680. || (mask->expr_type == EXPR_CONSTANT
  2681. && have_array_size)))
  2682. {
  2683. int mask_true_values = 0;
  2684. if (mask->expr_type == EXPR_ARRAY)
  2685. {
  2686. gfc_constructor *mask_ctor;
  2687. mask_ctor = gfc_constructor_first (mask->value.constructor);
  2688. while (mask_ctor)
  2689. {
  2690. if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
  2691. {
  2692. mask_true_values = 0;
  2693. break;
  2694. }
  2695. if (mask_ctor->expr->value.logical)
  2696. mask_true_values++;
  2697. mask_ctor = gfc_constructor_next (mask_ctor);
  2698. }
  2699. }
  2700. else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
  2701. mask_true_values = mpz_get_si (array_size);
  2702. if (mpz_get_si (vector_size) < mask_true_values)
  2703. {
  2704. gfc_error ("%qs argument of %qs intrinsic at %L must "
  2705. "provide at least as many elements as there "
  2706. "are .TRUE. values in %qs (%ld/%d)",
  2707. gfc_current_intrinsic_arg[2]->name,
  2708. gfc_current_intrinsic, &vector->where,
  2709. gfc_current_intrinsic_arg[1]->name,
  2710. mpz_get_si (vector_size), mask_true_values);
  2711. return false;
  2712. }
  2713. }
  2714. if (have_array_size)
  2715. mpz_clear (array_size);
  2716. if (have_vector_size)
  2717. mpz_clear (vector_size);
  2718. }
  2719. return true;
  2720. }
  2721. bool
  2722. gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
  2723. {
  2724. if (!type_check (mask, 0, BT_LOGICAL))
  2725. return false;
  2726. if (!array_check (mask, 0))
  2727. return false;
  2728. if (!dim_rank_check (dim, mask, false))
  2729. return false;
  2730. return true;
  2731. }
  2732. bool
  2733. gfc_check_precision (gfc_expr *x)
  2734. {
  2735. if (!real_or_complex_check (x, 0))
  2736. return false;
  2737. return true;
  2738. }
  2739. bool
  2740. gfc_check_present (gfc_expr *a)
  2741. {
  2742. gfc_symbol *sym;
  2743. if (!variable_check (a, 0, true))
  2744. return false;
  2745. sym = a->symtree->n.sym;
  2746. if (!sym->attr.dummy)
  2747. {
  2748. gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
  2749. "dummy variable", gfc_current_intrinsic_arg[0]->name,
  2750. gfc_current_intrinsic, &a->where);
  2751. return false;
  2752. }
  2753. if (!sym->attr.optional)
  2754. {
  2755. gfc_error ("%qs argument of %qs intrinsic at %L must be of "
  2756. "an OPTIONAL dummy variable",
  2757. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  2758. &a->where);
  2759. return false;
  2760. }
  2761. /* 13.14.82 PRESENT(A)
  2762. ......
  2763. Argument. A shall be the name of an optional dummy argument that is
  2764. accessible in the subprogram in which the PRESENT function reference
  2765. appears... */
  2766. if (a->ref != NULL
  2767. && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
  2768. && (a->ref->u.ar.type == AR_FULL
  2769. || (a->ref->u.ar.type == AR_ELEMENT
  2770. && a->ref->u.ar.as->rank == 0))))
  2771. {
  2772. gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
  2773. "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
  2774. gfc_current_intrinsic, &a->where, sym->name);
  2775. return false;
  2776. }
  2777. return true;
  2778. }
  2779. bool
  2780. gfc_check_radix (gfc_expr *x)
  2781. {
  2782. if (!int_or_real_check (x, 0))
  2783. return false;
  2784. return true;
  2785. }
  2786. bool
  2787. gfc_check_range (gfc_expr *x)
  2788. {
  2789. if (!numeric_check (x, 0))
  2790. return false;
  2791. return true;
  2792. }
  2793. bool
  2794. gfc_check_rank (gfc_expr *a ATTRIBUTE_UNUSED)
  2795. {
  2796. /* Any data object is allowed; a "data object" is a "constant (4.1.3),
  2797. variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
  2798. bool is_variable = true;
  2799. /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
  2800. if (a->expr_type == EXPR_FUNCTION)
  2801. is_variable = a->value.function.esym
  2802. ? a->value.function.esym->result->attr.pointer
  2803. : a->symtree->n.sym->result->attr.pointer;
  2804. if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
  2805. || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
  2806. || !is_variable)
  2807. {
  2808. gfc_error ("The argument of the RANK intrinsic at %L must be a data "
  2809. "object", &a->where);
  2810. return false;
  2811. }
  2812. return true;
  2813. }
  2814. /* real, float, sngl. */
  2815. bool
  2816. gfc_check_real (gfc_expr *a, gfc_expr *kind)
  2817. {
  2818. if (!numeric_check (a, 0))
  2819. return false;
  2820. if (!kind_check (kind, 1, BT_REAL))
  2821. return false;
  2822. return true;
  2823. }
  2824. bool
  2825. gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
  2826. {
  2827. if (!type_check (path1, 0, BT_CHARACTER))
  2828. return false;
  2829. if (!kind_value_check (path1, 0, gfc_default_character_kind))
  2830. return false;
  2831. if (!type_check (path2, 1, BT_CHARACTER))
  2832. return false;
  2833. if (!kind_value_check (path2, 1, gfc_default_character_kind))
  2834. return false;
  2835. return true;
  2836. }
  2837. bool
  2838. gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
  2839. {
  2840. if (!type_check (path1, 0, BT_CHARACTER))
  2841. return false;
  2842. if (!kind_value_check (path1, 0, gfc_default_character_kind))
  2843. return false;
  2844. if (!type_check (path2, 1, BT_CHARACTER))
  2845. return false;
  2846. if (!kind_value_check (path2, 1, gfc_default_character_kind))
  2847. return false;
  2848. if (status == NULL)
  2849. return true;
  2850. if (!type_check (status, 2, BT_INTEGER))
  2851. return false;
  2852. if (!scalar_check (status, 2))
  2853. return false;
  2854. return true;
  2855. }
  2856. bool
  2857. gfc_check_repeat (gfc_expr *x, gfc_expr *y)
  2858. {
  2859. if (!type_check (x, 0, BT_CHARACTER))
  2860. return false;
  2861. if (!scalar_check (x, 0))
  2862. return false;
  2863. if (!type_check (y, 0, BT_INTEGER))
  2864. return false;
  2865. if (!scalar_check (y, 1))
  2866. return false;
  2867. return true;
  2868. }
  2869. bool
  2870. gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
  2871. gfc_expr *pad, gfc_expr *order)
  2872. {
  2873. mpz_t size;
  2874. mpz_t nelems;
  2875. int shape_size;
  2876. if (!array_check (source, 0))
  2877. return false;
  2878. if (!rank_check (shape, 1, 1))
  2879. return false;
  2880. if (!type_check (shape, 1, BT_INTEGER))
  2881. return false;
  2882. if (!gfc_array_size (shape, &size))
  2883. {
  2884. gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
  2885. "array of constant size", &shape->where);
  2886. return false;
  2887. }
  2888. shape_size = mpz_get_ui (size);
  2889. mpz_clear (size);
  2890. if (shape_size <= 0)
  2891. {
  2892. gfc_error ("%qs argument of %qs intrinsic at %L is empty",
  2893. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  2894. &shape->where);
  2895. return false;
  2896. }
  2897. else if (shape_size > GFC_MAX_DIMENSIONS)
  2898. {
  2899. gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
  2900. "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
  2901. return false;
  2902. }
  2903. else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
  2904. {
  2905. gfc_expr *e;
  2906. int i, extent;
  2907. for (i = 0; i < shape_size; ++i)
  2908. {
  2909. e = gfc_constructor_lookup_expr (shape->value.constructor, i);
  2910. if (e->expr_type != EXPR_CONSTANT)
  2911. continue;
  2912. gfc_extract_int (e, &extent);
  2913. if (extent < 0)
  2914. {
  2915. gfc_error ("%qs argument of %qs intrinsic at %L has "
  2916. "negative element (%d)",
  2917. gfc_current_intrinsic_arg[1]->name,
  2918. gfc_current_intrinsic, &e->where, extent);
  2919. return false;
  2920. }
  2921. }
  2922. }
  2923. if (pad != NULL)
  2924. {
  2925. if (!same_type_check (source, 0, pad, 2))
  2926. return false;
  2927. if (!array_check (pad, 2))
  2928. return false;
  2929. }
  2930. if (order != NULL)
  2931. {
  2932. if (!array_check (order, 3))
  2933. return false;
  2934. if (!type_check (order, 3, BT_INTEGER))
  2935. return false;
  2936. if (order->expr_type == EXPR_ARRAY)
  2937. {
  2938. int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
  2939. gfc_expr *e;
  2940. for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
  2941. perm[i] = 0;
  2942. gfc_array_size (order, &size);
  2943. order_size = mpz_get_ui (size);
  2944. mpz_clear (size);
  2945. if (order_size != shape_size)
  2946. {
  2947. gfc_error ("%qs argument of %qs intrinsic at %L "
  2948. "has wrong number of elements (%d/%d)",
  2949. gfc_current_intrinsic_arg[3]->name,
  2950. gfc_current_intrinsic, &order->where,
  2951. order_size, shape_size);
  2952. return false;
  2953. }
  2954. for (i = 1; i <= order_size; ++i)
  2955. {
  2956. e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
  2957. if (e->expr_type != EXPR_CONSTANT)
  2958. continue;
  2959. gfc_extract_int (e, &dim);
  2960. if (dim < 1 || dim > order_size)
  2961. {
  2962. gfc_error ("%qs argument of %qs intrinsic at %L "
  2963. "has out-of-range dimension (%d)",
  2964. gfc_current_intrinsic_arg[3]->name,
  2965. gfc_current_intrinsic, &e->where, dim);
  2966. return false;
  2967. }
  2968. if (perm[dim-1] != 0)
  2969. {
  2970. gfc_error ("%qs argument of %qs intrinsic at %L has "
  2971. "invalid permutation of dimensions (dimension "
  2972. "%<%d%> duplicated)",
  2973. gfc_current_intrinsic_arg[3]->name,
  2974. gfc_current_intrinsic, &e->where, dim);
  2975. return false;
  2976. }
  2977. perm[dim-1] = 1;
  2978. }
  2979. }
  2980. }
  2981. if (pad == NULL && shape->expr_type == EXPR_ARRAY
  2982. && gfc_is_constant_expr (shape)
  2983. && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
  2984. && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
  2985. {
  2986. /* Check the match in size between source and destination. */
  2987. if (gfc_array_size (source, &nelems))
  2988. {
  2989. gfc_constructor *c;
  2990. bool test;
  2991. mpz_init_set_ui (size, 1);
  2992. for (c = gfc_constructor_first (shape->value.constructor);
  2993. c; c = gfc_constructor_next (c))
  2994. mpz_mul (size, size, c->expr->value.integer);
  2995. test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
  2996. mpz_clear (nelems);
  2997. mpz_clear (size);
  2998. if (test)
  2999. {
  3000. gfc_error ("Without padding, there are not enough elements "
  3001. "in the intrinsic RESHAPE source at %L to match "
  3002. "the shape", &source->where);
  3003. return false;
  3004. }
  3005. }
  3006. }
  3007. return true;
  3008. }
  3009. bool
  3010. gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
  3011. {
  3012. if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
  3013. {
  3014. gfc_error ("%qs argument of %qs intrinsic at %L "
  3015. "cannot be of type %s",
  3016. gfc_current_intrinsic_arg[0]->name,
  3017. gfc_current_intrinsic,
  3018. &a->where, gfc_typename (&a->ts));
  3019. return false;
  3020. }
  3021. if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
  3022. {
  3023. gfc_error ("%qs argument of %qs intrinsic at %L "
  3024. "must be of an extensible type",
  3025. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  3026. &a->where);
  3027. return false;
  3028. }
  3029. if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
  3030. {
  3031. gfc_error ("%qs argument of %qs intrinsic at %L "
  3032. "cannot be of type %s",
  3033. gfc_current_intrinsic_arg[0]->name,
  3034. gfc_current_intrinsic,
  3035. &b->where, gfc_typename (&b->ts));
  3036. return false;
  3037. }
  3038. if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
  3039. {
  3040. gfc_error ("%qs argument of %qs intrinsic at %L "
  3041. "must be of an extensible type",
  3042. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  3043. &b->where);
  3044. return false;
  3045. }
  3046. return true;
  3047. }
  3048. bool
  3049. gfc_check_scale (gfc_expr *x, gfc_expr *i)
  3050. {
  3051. if (!type_check (x, 0, BT_REAL))
  3052. return false;
  3053. if (!type_check (i, 1, BT_INTEGER))
  3054. return false;
  3055. return true;
  3056. }
  3057. bool
  3058. gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
  3059. {
  3060. if (!type_check (x, 0, BT_CHARACTER))
  3061. return false;
  3062. if (!type_check (y, 1, BT_CHARACTER))
  3063. return false;
  3064. if (z != NULL && !type_check (z, 2, BT_LOGICAL))
  3065. return false;
  3066. if (!kind_check (kind, 3, BT_INTEGER))
  3067. return false;
  3068. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  3069. "with KIND argument at %L",
  3070. gfc_current_intrinsic, &kind->where))
  3071. return false;
  3072. if (!same_type_check (x, 0, y, 1))
  3073. return false;
  3074. return true;
  3075. }
  3076. bool
  3077. gfc_check_secnds (gfc_expr *r)
  3078. {
  3079. if (!type_check (r, 0, BT_REAL))
  3080. return false;
  3081. if (!kind_value_check (r, 0, 4))
  3082. return false;
  3083. if (!scalar_check (r, 0))
  3084. return false;
  3085. return true;
  3086. }
  3087. bool
  3088. gfc_check_selected_char_kind (gfc_expr *name)
  3089. {
  3090. if (!type_check (name, 0, BT_CHARACTER))
  3091. return false;
  3092. if (!kind_value_check (name, 0, gfc_default_character_kind))
  3093. return false;
  3094. if (!scalar_check (name, 0))
  3095. return false;
  3096. return true;
  3097. }
  3098. bool
  3099. gfc_check_selected_int_kind (gfc_expr *r)
  3100. {
  3101. if (!type_check (r, 0, BT_INTEGER))
  3102. return false;
  3103. if (!scalar_check (r, 0))
  3104. return false;
  3105. return true;
  3106. }
  3107. bool
  3108. gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
  3109. {
  3110. if (p == NULL && r == NULL
  3111. && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
  3112. " neither %<P%> nor %<R%> argument at %L",
  3113. gfc_current_intrinsic_where))
  3114. return false;
  3115. if (p)
  3116. {
  3117. if (!type_check (p, 0, BT_INTEGER))
  3118. return false;
  3119. if (!scalar_check (p, 0))
  3120. return false;
  3121. }
  3122. if (r)
  3123. {
  3124. if (!type_check (r, 1, BT_INTEGER))
  3125. return false;
  3126. if (!scalar_check (r, 1))
  3127. return false;
  3128. }
  3129. if (radix)
  3130. {
  3131. if (!type_check (radix, 1, BT_INTEGER))
  3132. return false;
  3133. if (!scalar_check (radix, 1))
  3134. return false;
  3135. if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
  3136. "RADIX argument at %L", gfc_current_intrinsic,
  3137. &radix->where))
  3138. return false;
  3139. }
  3140. return true;
  3141. }
  3142. bool
  3143. gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
  3144. {
  3145. if (!type_check (x, 0, BT_REAL))
  3146. return false;
  3147. if (!type_check (i, 1, BT_INTEGER))
  3148. return false;
  3149. return true;
  3150. }
  3151. bool
  3152. gfc_check_shape (gfc_expr *source, gfc_expr *kind)
  3153. {
  3154. gfc_array_ref *ar;
  3155. if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
  3156. return true;
  3157. ar = gfc_find_array_ref (source);
  3158. if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
  3159. {
  3160. gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
  3161. "an assumed size array", &source->where);
  3162. return false;
  3163. }
  3164. if (!kind_check (kind, 1, BT_INTEGER))
  3165. return false;
  3166. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  3167. "with KIND argument at %L",
  3168. gfc_current_intrinsic, &kind->where))
  3169. return false;
  3170. return true;
  3171. }
  3172. bool
  3173. gfc_check_shift (gfc_expr *i, gfc_expr *shift)
  3174. {
  3175. if (!type_check (i, 0, BT_INTEGER))
  3176. return false;
  3177. if (!type_check (shift, 0, BT_INTEGER))
  3178. return false;
  3179. if (!nonnegative_check ("SHIFT", shift))
  3180. return false;
  3181. if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
  3182. return false;
  3183. return true;
  3184. }
  3185. bool
  3186. gfc_check_sign (gfc_expr *a, gfc_expr *b)
  3187. {
  3188. if (!int_or_real_check (a, 0))
  3189. return false;
  3190. if (!same_type_check (a, 0, b, 1))
  3191. return false;
  3192. return true;
  3193. }
  3194. bool
  3195. gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
  3196. {
  3197. if (!array_check (array, 0))
  3198. return false;
  3199. if (!dim_check (dim, 1, true))
  3200. return false;
  3201. if (!dim_rank_check (dim, array, 0))
  3202. return false;
  3203. if (!kind_check (kind, 2, BT_INTEGER))
  3204. return false;
  3205. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  3206. "with KIND argument at %L",
  3207. gfc_current_intrinsic, &kind->where))
  3208. return false;
  3209. return true;
  3210. }
  3211. bool
  3212. gfc_check_sizeof (gfc_expr *arg)
  3213. {
  3214. if (arg->ts.type == BT_PROCEDURE)
  3215. {
  3216. gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
  3217. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  3218. &arg->where);
  3219. return false;
  3220. }
  3221. /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
  3222. if (arg->ts.type == BT_ASSUMED
  3223. && (arg->symtree->n.sym->as == NULL
  3224. || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
  3225. && arg->symtree->n.sym->as->type != AS_DEFERRED
  3226. && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
  3227. {
  3228. gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
  3229. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  3230. &arg->where);
  3231. return false;
  3232. }
  3233. if (arg->rank && arg->expr_type == EXPR_VARIABLE
  3234. && arg->symtree->n.sym->as != NULL
  3235. && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
  3236. && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
  3237. {
  3238. gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
  3239. "assumed-size array", gfc_current_intrinsic_arg[0]->name,
  3240. gfc_current_intrinsic, &arg->where);
  3241. return false;
  3242. }
  3243. return true;
  3244. }
  3245. /* Check whether an expression is interoperable. When returning false,
  3246. msg is set to a string telling why the expression is not interoperable,
  3247. otherwise, it is set to NULL. The msg string can be used in diagnostics.
  3248. If c_loc is true, character with len > 1 are allowed (cf. Fortran
  3249. 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
  3250. arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
  3251. are permitted. */
  3252. static bool
  3253. is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
  3254. {
  3255. *msg = NULL;
  3256. if (expr->ts.type == BT_CLASS)
  3257. {
  3258. *msg = "Expression is polymorphic";
  3259. return false;
  3260. }
  3261. if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
  3262. && !expr->ts.u.derived->ts.is_iso_c)
  3263. {
  3264. *msg = "Expression is a noninteroperable derived type";
  3265. return false;
  3266. }
  3267. if (expr->ts.type == BT_PROCEDURE)
  3268. {
  3269. *msg = "Procedure unexpected as argument";
  3270. return false;
  3271. }
  3272. if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
  3273. {
  3274. int i;
  3275. for (i = 0; gfc_logical_kinds[i].kind; i++)
  3276. if (gfc_logical_kinds[i].kind == expr->ts.kind)
  3277. return true;
  3278. *msg = "Extension to use a non-C_Bool-kind LOGICAL";
  3279. return false;
  3280. }
  3281. if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
  3282. && expr->ts.kind != 1)
  3283. {
  3284. *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
  3285. return false;
  3286. }
  3287. if (expr->ts.type == BT_CHARACTER) {
  3288. if (expr->ts.deferred)
  3289. {
  3290. /* TS 29113 allows deferred-length strings as dummy arguments,
  3291. but it is not an interoperable type. */
  3292. *msg = "Expression shall not be a deferred-length string";
  3293. return false;
  3294. }
  3295. if (expr->ts.u.cl && expr->ts.u.cl->length
  3296. && !gfc_simplify_expr (expr, 0))
  3297. gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
  3298. if (!c_loc && expr->ts.u.cl
  3299. && (!expr->ts.u.cl->length
  3300. || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
  3301. || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
  3302. {
  3303. *msg = "Type shall have a character length of 1";
  3304. return false;
  3305. }
  3306. }
  3307. /* Note: The following checks are about interoperatable variables, Fortran
  3308. 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
  3309. is allowed, e.g. assumed-shape arrays with TS 29113. */
  3310. if (gfc_is_coarray (expr))
  3311. {
  3312. *msg = "Coarrays are not interoperable";
  3313. return false;
  3314. }
  3315. if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
  3316. {
  3317. gfc_array_ref *ar = gfc_find_array_ref (expr);
  3318. if (ar->type != AR_FULL)
  3319. {
  3320. *msg = "Only whole-arrays are interoperable";
  3321. return false;
  3322. }
  3323. if (!c_f_ptr && ar->as->type != AS_EXPLICIT
  3324. && ar->as->type != AS_ASSUMED_SIZE)
  3325. {
  3326. *msg = "Only explicit-size and assumed-size arrays are interoperable";
  3327. return false;
  3328. }
  3329. }
  3330. return true;
  3331. }
  3332. bool
  3333. gfc_check_c_sizeof (gfc_expr *arg)
  3334. {
  3335. const char *msg;
  3336. if (!is_c_interoperable (arg, &msg, false, false))
  3337. {
  3338. gfc_error ("%qs argument of %qs intrinsic at %L must be an "
  3339. "interoperable data entity: %s",
  3340. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  3341. &arg->where, msg);
  3342. return false;
  3343. }
  3344. if (arg->ts.type == BT_ASSUMED)
  3345. {
  3346. gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
  3347. "TYPE(*)",
  3348. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  3349. &arg->where);
  3350. return false;
  3351. }
  3352. if (arg->rank && arg->expr_type == EXPR_VARIABLE
  3353. && arg->symtree->n.sym->as != NULL
  3354. && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
  3355. && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
  3356. {
  3357. gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
  3358. "assumed-size array", gfc_current_intrinsic_arg[0]->name,
  3359. gfc_current_intrinsic, &arg->where);
  3360. return false;
  3361. }
  3362. return true;
  3363. }
  3364. bool
  3365. gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
  3366. {
  3367. if (c_ptr_1->ts.type != BT_DERIVED
  3368. || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
  3369. || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
  3370. && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
  3371. {
  3372. gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
  3373. "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
  3374. return false;
  3375. }
  3376. if (!scalar_check (c_ptr_1, 0))
  3377. return false;
  3378. if (c_ptr_2
  3379. && (c_ptr_2->ts.type != BT_DERIVED
  3380. || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
  3381. || (c_ptr_1->ts.u.derived->intmod_sym_id
  3382. != c_ptr_2->ts.u.derived->intmod_sym_id)))
  3383. {
  3384. gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
  3385. "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
  3386. gfc_typename (&c_ptr_1->ts),
  3387. gfc_typename (&c_ptr_2->ts));
  3388. return false;
  3389. }
  3390. if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
  3391. return false;
  3392. return true;
  3393. }
  3394. bool
  3395. gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
  3396. {
  3397. symbol_attribute attr;
  3398. const char *msg;
  3399. if (cptr->ts.type != BT_DERIVED
  3400. || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
  3401. || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
  3402. {
  3403. gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
  3404. "type TYPE(C_PTR)", &cptr->where);
  3405. return false;
  3406. }
  3407. if (!scalar_check (cptr, 0))
  3408. return false;
  3409. attr = gfc_expr_attr (fptr);
  3410. if (!attr.pointer)
  3411. {
  3412. gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
  3413. &fptr->where);
  3414. return false;
  3415. }
  3416. if (fptr->ts.type == BT_CLASS)
  3417. {
  3418. gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
  3419. &fptr->where);
  3420. return false;
  3421. }
  3422. if (gfc_is_coindexed (fptr))
  3423. {
  3424. gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
  3425. "coindexed", &fptr->where);
  3426. return false;
  3427. }
  3428. if (fptr->rank == 0 && shape)
  3429. {
  3430. gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
  3431. "FPTR", &fptr->where);
  3432. return false;
  3433. }
  3434. else if (fptr->rank && !shape)
  3435. {
  3436. gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
  3437. "FPTR at %L", &fptr->where);
  3438. return false;
  3439. }
  3440. if (shape && !rank_check (shape, 2, 1))
  3441. return false;
  3442. if (shape && !type_check (shape, 2, BT_INTEGER))
  3443. return false;
  3444. if (shape)
  3445. {
  3446. mpz_t size;
  3447. if (gfc_array_size (shape, &size))
  3448. {
  3449. if (mpz_cmp_ui (size, fptr->rank) != 0)
  3450. {
  3451. mpz_clear (size);
  3452. gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
  3453. "size as the RANK of FPTR", &shape->where);
  3454. return false;
  3455. }
  3456. mpz_clear (size);
  3457. }
  3458. }
  3459. if (fptr->ts.type == BT_CLASS)
  3460. {
  3461. gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
  3462. return false;
  3463. }
  3464. if (!is_c_interoperable (fptr, &msg, false, true))
  3465. return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
  3466. "at %L to C_F_POINTER: %s", &fptr->where, msg);
  3467. return true;
  3468. }
  3469. bool
  3470. gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
  3471. {
  3472. symbol_attribute attr;
  3473. if (cptr->ts.type != BT_DERIVED
  3474. || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
  3475. || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
  3476. {
  3477. gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
  3478. "type TYPE(C_FUNPTR)", &cptr->where);
  3479. return false;
  3480. }
  3481. if (!scalar_check (cptr, 0))
  3482. return false;
  3483. attr = gfc_expr_attr (fptr);
  3484. if (!attr.proc_pointer)
  3485. {
  3486. gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
  3487. "pointer", &fptr->where);
  3488. return false;
  3489. }
  3490. if (gfc_is_coindexed (fptr))
  3491. {
  3492. gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
  3493. "coindexed", &fptr->where);
  3494. return false;
  3495. }
  3496. if (!attr.is_bind_c)
  3497. return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
  3498. "pointer at %L to C_F_PROCPOINTER", &fptr->where);
  3499. return true;
  3500. }
  3501. bool
  3502. gfc_check_c_funloc (gfc_expr *x)
  3503. {
  3504. symbol_attribute attr;
  3505. if (gfc_is_coindexed (x))
  3506. {
  3507. gfc_error ("Argument X at %L to C_FUNLOC shall not be "
  3508. "coindexed", &x->where);
  3509. return false;
  3510. }
  3511. attr = gfc_expr_attr (x);
  3512. if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
  3513. && x->symtree->n.sym == x->symtree->n.sym->result)
  3514. {
  3515. gfc_namespace *ns = gfc_current_ns;
  3516. for (ns = gfc_current_ns; ns; ns = ns->parent)
  3517. if (x->symtree->n.sym == ns->proc_name)
  3518. {
  3519. gfc_error ("Function result %qs at %L is invalid as X argument "
  3520. "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
  3521. return false;
  3522. }
  3523. }
  3524. if (attr.flavor != FL_PROCEDURE)
  3525. {
  3526. gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
  3527. "or a procedure pointer", &x->where);
  3528. return false;
  3529. }
  3530. if (!attr.is_bind_c)
  3531. return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
  3532. "at %L to C_FUNLOC", &x->where);
  3533. return true;
  3534. }
  3535. bool
  3536. gfc_check_c_loc (gfc_expr *x)
  3537. {
  3538. symbol_attribute attr;
  3539. const char *msg;
  3540. if (gfc_is_coindexed (x))
  3541. {
  3542. gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
  3543. return false;
  3544. }
  3545. if (x->ts.type == BT_CLASS)
  3546. {
  3547. gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
  3548. &x->where);
  3549. return false;
  3550. }
  3551. attr = gfc_expr_attr (x);
  3552. if (!attr.pointer
  3553. && (x->expr_type != EXPR_VARIABLE || !attr.target
  3554. || attr.flavor == FL_PARAMETER))
  3555. {
  3556. gfc_error ("Argument X at %L to C_LOC shall have either "
  3557. "the POINTER or the TARGET attribute", &x->where);
  3558. return false;
  3559. }
  3560. if (x->ts.type == BT_CHARACTER
  3561. && gfc_var_strlen (x) == 0)
  3562. {
  3563. gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
  3564. "string", &x->where);
  3565. return false;
  3566. }
  3567. if (!is_c_interoperable (x, &msg, true, false))
  3568. {
  3569. if (x->ts.type == BT_CLASS)
  3570. {
  3571. gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
  3572. &x->where);
  3573. return false;
  3574. }
  3575. if (x->rank
  3576. && !gfc_notify_std (GFC_STD_F2008_TS,
  3577. "Noninteroperable array at %L as"
  3578. " argument to C_LOC: %s", &x->where, msg))
  3579. return false;
  3580. }
  3581. else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
  3582. {
  3583. gfc_array_ref *ar = gfc_find_array_ref (x);
  3584. if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
  3585. && !attr.allocatable
  3586. && !gfc_notify_std (GFC_STD_F2008,
  3587. "Array of interoperable type at %L "
  3588. "to C_LOC which is nonallocatable and neither "
  3589. "assumed size nor explicit size", &x->where))
  3590. return false;
  3591. else if (ar->type != AR_FULL
  3592. && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
  3593. "to C_LOC", &x->where))
  3594. return false;
  3595. }
  3596. return true;
  3597. }
  3598. bool
  3599. gfc_check_sleep_sub (gfc_expr *seconds)
  3600. {
  3601. if (!type_check (seconds, 0, BT_INTEGER))
  3602. return false;
  3603. if (!scalar_check (seconds, 0))
  3604. return false;
  3605. return true;
  3606. }
  3607. bool
  3608. gfc_check_sngl (gfc_expr *a)
  3609. {
  3610. if (!type_check (a, 0, BT_REAL))
  3611. return false;
  3612. if ((a->ts.kind != gfc_default_double_kind)
  3613. && !gfc_notify_std (GFC_STD_GNU, "non double precision "
  3614. "REAL argument to %s intrinsic at %L",
  3615. gfc_current_intrinsic, &a->where))
  3616. return false;
  3617. return true;
  3618. }
  3619. bool
  3620. gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
  3621. {
  3622. if (source->rank >= GFC_MAX_DIMENSIONS)
  3623. {
  3624. gfc_error ("%qs argument of %qs intrinsic at %L must be less "
  3625. "than rank %d", gfc_current_intrinsic_arg[0]->name,
  3626. gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
  3627. return false;
  3628. }
  3629. if (dim == NULL)
  3630. return false;
  3631. if (!dim_check (dim, 1, false))
  3632. return false;
  3633. /* dim_rank_check() does not apply here. */
  3634. if (dim
  3635. && dim->expr_type == EXPR_CONSTANT
  3636. && (mpz_cmp_ui (dim->value.integer, 1) < 0
  3637. || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
  3638. {
  3639. gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
  3640. "dimension index", gfc_current_intrinsic_arg[1]->name,
  3641. gfc_current_intrinsic, &dim->where);
  3642. return false;
  3643. }
  3644. if (!type_check (ncopies, 2, BT_INTEGER))
  3645. return false;
  3646. if (!scalar_check (ncopies, 2))
  3647. return false;
  3648. return true;
  3649. }
  3650. /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
  3651. functions). */
  3652. bool
  3653. gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
  3654. {
  3655. if (!type_check (unit, 0, BT_INTEGER))
  3656. return false;
  3657. if (!scalar_check (unit, 0))
  3658. return false;
  3659. if (!type_check (c, 1, BT_CHARACTER))
  3660. return false;
  3661. if (!kind_value_check (c, 1, gfc_default_character_kind))
  3662. return false;
  3663. if (status == NULL)
  3664. return true;
  3665. if (!type_check (status, 2, BT_INTEGER)
  3666. || !kind_value_check (status, 2, gfc_default_integer_kind)
  3667. || !scalar_check (status, 2))
  3668. return false;
  3669. return true;
  3670. }
  3671. bool
  3672. gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
  3673. {
  3674. return gfc_check_fgetputc_sub (unit, c, NULL);
  3675. }
  3676. bool
  3677. gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
  3678. {
  3679. if (!type_check (c, 0, BT_CHARACTER))
  3680. return false;
  3681. if (!kind_value_check (c, 0, gfc_default_character_kind))
  3682. return false;
  3683. if (status == NULL)
  3684. return true;
  3685. if (!type_check (status, 1, BT_INTEGER)
  3686. || !kind_value_check (status, 1, gfc_default_integer_kind)
  3687. || !scalar_check (status, 1))
  3688. return false;
  3689. return true;
  3690. }
  3691. bool
  3692. gfc_check_fgetput (gfc_expr *c)
  3693. {
  3694. return gfc_check_fgetput_sub (c, NULL);
  3695. }
  3696. bool
  3697. gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
  3698. {
  3699. if (!type_check (unit, 0, BT_INTEGER))
  3700. return false;
  3701. if (!scalar_check (unit, 0))
  3702. return false;
  3703. if (!type_check (offset, 1, BT_INTEGER))
  3704. return false;
  3705. if (!scalar_check (offset, 1))
  3706. return false;
  3707. if (!type_check (whence, 2, BT_INTEGER))
  3708. return false;
  3709. if (!scalar_check (whence, 2))
  3710. return false;
  3711. if (status == NULL)
  3712. return true;
  3713. if (!type_check (status, 3, BT_INTEGER))
  3714. return false;
  3715. if (!kind_value_check (status, 3, 4))
  3716. return false;
  3717. if (!scalar_check (status, 3))
  3718. return false;
  3719. return true;
  3720. }
  3721. bool
  3722. gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
  3723. {
  3724. if (!type_check (unit, 0, BT_INTEGER))
  3725. return false;
  3726. if (!scalar_check (unit, 0))
  3727. return false;
  3728. if (!type_check (array, 1, BT_INTEGER)
  3729. || !kind_value_check (unit, 0, gfc_default_integer_kind))
  3730. return false;
  3731. if (!array_check (array, 1))
  3732. return false;
  3733. return true;
  3734. }
  3735. bool
  3736. gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
  3737. {
  3738. if (!type_check (unit, 0, BT_INTEGER))
  3739. return false;
  3740. if (!scalar_check (unit, 0))
  3741. return false;
  3742. if (!type_check (array, 1, BT_INTEGER)
  3743. || !kind_value_check (array, 1, gfc_default_integer_kind))
  3744. return false;
  3745. if (!array_check (array, 1))
  3746. return false;
  3747. if (status == NULL)
  3748. return true;
  3749. if (!type_check (status, 2, BT_INTEGER)
  3750. || !kind_value_check (status, 2, gfc_default_integer_kind))
  3751. return false;
  3752. if (!scalar_check (status, 2))
  3753. return false;
  3754. return true;
  3755. }
  3756. bool
  3757. gfc_check_ftell (gfc_expr *unit)
  3758. {
  3759. if (!type_check (unit, 0, BT_INTEGER))
  3760. return false;
  3761. if (!scalar_check (unit, 0))
  3762. return false;
  3763. return true;
  3764. }
  3765. bool
  3766. gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
  3767. {
  3768. if (!type_check (unit, 0, BT_INTEGER))
  3769. return false;
  3770. if (!scalar_check (unit, 0))
  3771. return false;
  3772. if (!type_check (offset, 1, BT_INTEGER))
  3773. return false;
  3774. if (!scalar_check (offset, 1))
  3775. return false;
  3776. return true;
  3777. }
  3778. bool
  3779. gfc_check_stat (gfc_expr *name, gfc_expr *array)
  3780. {
  3781. if (!type_check (name, 0, BT_CHARACTER))
  3782. return false;
  3783. if (!kind_value_check (name, 0, gfc_default_character_kind))
  3784. return false;
  3785. if (!type_check (array, 1, BT_INTEGER)
  3786. || !kind_value_check (array, 1, gfc_default_integer_kind))
  3787. return false;
  3788. if (!array_check (array, 1))
  3789. return false;
  3790. return true;
  3791. }
  3792. bool
  3793. gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
  3794. {
  3795. if (!type_check (name, 0, BT_CHARACTER))
  3796. return false;
  3797. if (!kind_value_check (name, 0, gfc_default_character_kind))
  3798. return false;
  3799. if (!type_check (array, 1, BT_INTEGER)
  3800. || !kind_value_check (array, 1, gfc_default_integer_kind))
  3801. return false;
  3802. if (!array_check (array, 1))
  3803. return false;
  3804. if (status == NULL)
  3805. return true;
  3806. if (!type_check (status, 2, BT_INTEGER)
  3807. || !kind_value_check (array, 1, gfc_default_integer_kind))
  3808. return false;
  3809. if (!scalar_check (status, 2))
  3810. return false;
  3811. return true;
  3812. }
  3813. bool
  3814. gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
  3815. {
  3816. mpz_t nelems;
  3817. if (flag_coarray == GFC_FCOARRAY_NONE)
  3818. {
  3819. gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
  3820. return false;
  3821. }
  3822. if (!coarray_check (coarray, 0))
  3823. return false;
  3824. if (sub->rank != 1)
  3825. {
  3826. gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
  3827. gfc_current_intrinsic_arg[1]->name, &sub->where);
  3828. return false;
  3829. }
  3830. if (gfc_array_size (sub, &nelems))
  3831. {
  3832. int corank = gfc_get_corank (coarray);
  3833. if (mpz_cmp_ui (nelems, corank) != 0)
  3834. {
  3835. gfc_error ("The number of array elements of the SUB argument to "
  3836. "IMAGE_INDEX at %L shall be %d (corank) not %d",
  3837. &sub->where, corank, (int) mpz_get_si (nelems));
  3838. mpz_clear (nelems);
  3839. return false;
  3840. }
  3841. mpz_clear (nelems);
  3842. }
  3843. return true;
  3844. }
  3845. bool
  3846. gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
  3847. {
  3848. if (flag_coarray == GFC_FCOARRAY_NONE)
  3849. {
  3850. gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
  3851. return false;
  3852. }
  3853. if (distance)
  3854. {
  3855. if (!type_check (distance, 0, BT_INTEGER))
  3856. return false;
  3857. if (!nonnegative_check ("DISTANCE", distance))
  3858. return false;
  3859. if (!scalar_check (distance, 0))
  3860. return false;
  3861. if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
  3862. "NUM_IMAGES at %L", &distance->where))
  3863. return false;
  3864. }
  3865. if (failed)
  3866. {
  3867. if (!type_check (failed, 1, BT_LOGICAL))
  3868. return false;
  3869. if (!scalar_check (failed, 1))
  3870. return false;
  3871. if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
  3872. "NUM_IMAGES at %L", &distance->where))
  3873. return false;
  3874. }
  3875. return true;
  3876. }
  3877. bool
  3878. gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
  3879. {
  3880. if (flag_coarray == GFC_FCOARRAY_NONE)
  3881. {
  3882. gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
  3883. return false;
  3884. }
  3885. if (coarray == NULL && dim == NULL && distance == NULL)
  3886. return true;
  3887. if (dim != NULL && coarray == NULL)
  3888. {
  3889. gfc_error ("DIM argument without COARRAY argument not allowed for "
  3890. "THIS_IMAGE intrinsic at %L", &dim->where);
  3891. return false;
  3892. }
  3893. if (distance && (coarray || dim))
  3894. {
  3895. gfc_error ("The DISTANCE argument may not be specified together with the "
  3896. "COARRAY or DIM argument in intrinsic at %L",
  3897. &distance->where);
  3898. return false;
  3899. }
  3900. /* Assume that we have "this_image (distance)". */
  3901. if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
  3902. {
  3903. if (dim)
  3904. {
  3905. gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
  3906. &coarray->where);
  3907. return false;
  3908. }
  3909. distance = coarray;
  3910. }
  3911. if (distance)
  3912. {
  3913. if (!type_check (distance, 2, BT_INTEGER))
  3914. return false;
  3915. if (!nonnegative_check ("DISTANCE", distance))
  3916. return false;
  3917. if (!scalar_check (distance, 2))
  3918. return false;
  3919. if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
  3920. "THIS_IMAGE at %L", &distance->where))
  3921. return false;
  3922. return true;
  3923. }
  3924. if (!coarray_check (coarray, 0))
  3925. return false;
  3926. if (dim != NULL)
  3927. {
  3928. if (!dim_check (dim, 1, false))
  3929. return false;
  3930. if (!dim_corank_check (dim, coarray))
  3931. return false;
  3932. }
  3933. return true;
  3934. }
  3935. /* Calculate the sizes for transfer, used by gfc_check_transfer and also
  3936. by gfc_simplify_transfer. Return false if we cannot do so. */
  3937. bool
  3938. gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
  3939. size_t *source_size, size_t *result_size,
  3940. size_t *result_length_p)
  3941. {
  3942. size_t result_elt_size;
  3943. if (source->expr_type == EXPR_FUNCTION)
  3944. return false;
  3945. if (size && size->expr_type != EXPR_CONSTANT)
  3946. return false;
  3947. /* Calculate the size of the source. */
  3948. *source_size = gfc_target_expr_size (source);
  3949. if (*source_size == 0)
  3950. return false;
  3951. /* Determine the size of the element. */
  3952. result_elt_size = gfc_element_size (mold);
  3953. if (result_elt_size == 0)
  3954. return false;
  3955. if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
  3956. {
  3957. int result_length;
  3958. if (size)
  3959. result_length = (size_t)mpz_get_ui (size->value.integer);
  3960. else
  3961. {
  3962. result_length = *source_size / result_elt_size;
  3963. if (result_length * result_elt_size < *source_size)
  3964. result_length += 1;
  3965. }
  3966. *result_size = result_length * result_elt_size;
  3967. if (result_length_p)
  3968. *result_length_p = result_length;
  3969. }
  3970. else
  3971. *result_size = result_elt_size;
  3972. return true;
  3973. }
  3974. bool
  3975. gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
  3976. {
  3977. size_t source_size;
  3978. size_t result_size;
  3979. if (mold->ts.type == BT_HOLLERITH)
  3980. {
  3981. gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
  3982. " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
  3983. return false;
  3984. }
  3985. if (size != NULL)
  3986. {
  3987. if (!type_check (size, 2, BT_INTEGER))
  3988. return false;
  3989. if (!scalar_check (size, 2))
  3990. return false;
  3991. if (!nonoptional_check (size, 2))
  3992. return false;
  3993. }
  3994. if (!warn_surprising)
  3995. return true;
  3996. /* If we can't calculate the sizes, we cannot check any more.
  3997. Return true for that case. */
  3998. if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
  3999. &result_size, NULL))
  4000. return true;
  4001. if (source_size < result_size)
  4002. gfc_warning (0, "Intrinsic TRANSFER at %L has partly undefined result: "
  4003. "source size %ld < result size %ld", &source->where,
  4004. (long) source_size, (long) result_size);
  4005. return true;
  4006. }
  4007. bool
  4008. gfc_check_transpose (gfc_expr *matrix)
  4009. {
  4010. if (!rank_check (matrix, 0, 2))
  4011. return false;
  4012. return true;
  4013. }
  4014. bool
  4015. gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
  4016. {
  4017. if (!array_check (array, 0))
  4018. return false;
  4019. if (!dim_check (dim, 1, false))
  4020. return false;
  4021. if (!dim_rank_check (dim, array, 0))
  4022. return false;
  4023. if (!kind_check (kind, 2, BT_INTEGER))
  4024. return false;
  4025. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  4026. "with KIND argument at %L",
  4027. gfc_current_intrinsic, &kind->where))
  4028. return false;
  4029. return true;
  4030. }
  4031. bool
  4032. gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
  4033. {
  4034. if (flag_coarray == GFC_FCOARRAY_NONE)
  4035. {
  4036. gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
  4037. return false;
  4038. }
  4039. if (!coarray_check (coarray, 0))
  4040. return false;
  4041. if (dim != NULL)
  4042. {
  4043. if (!dim_check (dim, 1, false))
  4044. return false;
  4045. if (!dim_corank_check (dim, coarray))
  4046. return false;
  4047. }
  4048. if (!kind_check (kind, 2, BT_INTEGER))
  4049. return false;
  4050. return true;
  4051. }
  4052. bool
  4053. gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
  4054. {
  4055. mpz_t vector_size;
  4056. if (!rank_check (vector, 0, 1))
  4057. return false;
  4058. if (!array_check (mask, 1))
  4059. return false;
  4060. if (!type_check (mask, 1, BT_LOGICAL))
  4061. return false;
  4062. if (!same_type_check (vector, 0, field, 2))
  4063. return false;
  4064. if (mask->expr_type == EXPR_ARRAY
  4065. && gfc_array_size (vector, &vector_size))
  4066. {
  4067. int mask_true_count = 0;
  4068. gfc_constructor *mask_ctor;
  4069. mask_ctor = gfc_constructor_first (mask->value.constructor);
  4070. while (mask_ctor)
  4071. {
  4072. if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
  4073. {
  4074. mask_true_count = 0;
  4075. break;
  4076. }
  4077. if (mask_ctor->expr->value.logical)
  4078. mask_true_count++;
  4079. mask_ctor = gfc_constructor_next (mask_ctor);
  4080. }
  4081. if (mpz_get_si (vector_size) < mask_true_count)
  4082. {
  4083. gfc_error ("%qs argument of %qs intrinsic at %L must "
  4084. "provide at least as many elements as there "
  4085. "are .TRUE. values in %qs (%ld/%d)",
  4086. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  4087. &vector->where, gfc_current_intrinsic_arg[1]->name,
  4088. mpz_get_si (vector_size), mask_true_count);
  4089. return false;
  4090. }
  4091. mpz_clear (vector_size);
  4092. }
  4093. if (mask->rank != field->rank && field->rank != 0)
  4094. {
  4095. gfc_error ("%qs argument of %qs intrinsic at %L must have "
  4096. "the same rank as %qs or be a scalar",
  4097. gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
  4098. &field->where, gfc_current_intrinsic_arg[1]->name);
  4099. return false;
  4100. }
  4101. if (mask->rank == field->rank)
  4102. {
  4103. int i;
  4104. for (i = 0; i < field->rank; i++)
  4105. if (! identical_dimen_shape (mask, i, field, i))
  4106. {
  4107. gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
  4108. "must have identical shape.",
  4109. gfc_current_intrinsic_arg[2]->name,
  4110. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  4111. &field->where);
  4112. }
  4113. }
  4114. return true;
  4115. }
  4116. bool
  4117. gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
  4118. {
  4119. if (!type_check (x, 0, BT_CHARACTER))
  4120. return false;
  4121. if (!same_type_check (x, 0, y, 1))
  4122. return false;
  4123. if (z != NULL && !type_check (z, 2, BT_LOGICAL))
  4124. return false;
  4125. if (!kind_check (kind, 3, BT_INTEGER))
  4126. return false;
  4127. if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
  4128. "with KIND argument at %L",
  4129. gfc_current_intrinsic, &kind->where))
  4130. return false;
  4131. return true;
  4132. }
  4133. bool
  4134. gfc_check_trim (gfc_expr *x)
  4135. {
  4136. if (!type_check (x, 0, BT_CHARACTER))
  4137. return false;
  4138. if (!scalar_check (x, 0))
  4139. return false;
  4140. return true;
  4141. }
  4142. bool
  4143. gfc_check_ttynam (gfc_expr *unit)
  4144. {
  4145. if (!scalar_check (unit, 0))
  4146. return false;
  4147. if (!type_check (unit, 0, BT_INTEGER))
  4148. return false;
  4149. return true;
  4150. }
  4151. /* Common check function for the half a dozen intrinsics that have a
  4152. single real argument. */
  4153. bool
  4154. gfc_check_x (gfc_expr *x)
  4155. {
  4156. if (!type_check (x, 0, BT_REAL))
  4157. return false;
  4158. return true;
  4159. }
  4160. /************* Check functions for intrinsic subroutines *************/
  4161. bool
  4162. gfc_check_cpu_time (gfc_expr *time)
  4163. {
  4164. if (!scalar_check (time, 0))
  4165. return false;
  4166. if (!type_check (time, 0, BT_REAL))
  4167. return false;
  4168. if (!variable_check (time, 0, false))
  4169. return false;
  4170. return true;
  4171. }
  4172. bool
  4173. gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
  4174. gfc_expr *zone, gfc_expr *values)
  4175. {
  4176. if (date != NULL)
  4177. {
  4178. if (!type_check (date, 0, BT_CHARACTER))
  4179. return false;
  4180. if (!kind_value_check (date, 0, gfc_default_character_kind))
  4181. return false;
  4182. if (!scalar_check (date, 0))
  4183. return false;
  4184. if (!variable_check (date, 0, false))
  4185. return false;
  4186. }
  4187. if (time != NULL)
  4188. {
  4189. if (!type_check (time, 1, BT_CHARACTER))
  4190. return false;
  4191. if (!kind_value_check (time, 1, gfc_default_character_kind))
  4192. return false;
  4193. if (!scalar_check (time, 1))
  4194. return false;
  4195. if (!variable_check (time, 1, false))
  4196. return false;
  4197. }
  4198. if (zone != NULL)
  4199. {
  4200. if (!type_check (zone, 2, BT_CHARACTER))
  4201. return false;
  4202. if (!kind_value_check (zone, 2, gfc_default_character_kind))
  4203. return false;
  4204. if (!scalar_check (zone, 2))
  4205. return false;
  4206. if (!variable_check (zone, 2, false))
  4207. return false;
  4208. }
  4209. if (values != NULL)
  4210. {
  4211. if (!type_check (values, 3, BT_INTEGER))
  4212. return false;
  4213. if (!array_check (values, 3))
  4214. return false;
  4215. if (!rank_check (values, 3, 1))
  4216. return false;
  4217. if (!variable_check (values, 3, false))
  4218. return false;
  4219. }
  4220. return true;
  4221. }
  4222. bool
  4223. gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
  4224. gfc_expr *to, gfc_expr *topos)
  4225. {
  4226. if (!type_check (from, 0, BT_INTEGER))
  4227. return false;
  4228. if (!type_check (frompos, 1, BT_INTEGER))
  4229. return false;
  4230. if (!type_check (len, 2, BT_INTEGER))
  4231. return false;
  4232. if (!same_type_check (from, 0, to, 3))
  4233. return false;
  4234. if (!variable_check (to, 3, false))
  4235. return false;
  4236. if (!type_check (topos, 4, BT_INTEGER))
  4237. return false;
  4238. if (!nonnegative_check ("frompos", frompos))
  4239. return false;
  4240. if (!nonnegative_check ("topos", topos))
  4241. return false;
  4242. if (!nonnegative_check ("len", len))
  4243. return false;
  4244. if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
  4245. return false;
  4246. if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
  4247. return false;
  4248. return true;
  4249. }
  4250. bool
  4251. gfc_check_random_number (gfc_expr *harvest)
  4252. {
  4253. if (!type_check (harvest, 0, BT_REAL))
  4254. return false;
  4255. if (!variable_check (harvest, 0, false))
  4256. return false;
  4257. return true;
  4258. }
  4259. bool
  4260. gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
  4261. {
  4262. unsigned int nargs = 0, kiss_size;
  4263. locus *where = NULL;
  4264. mpz_t put_size, get_size;
  4265. bool have_gfc_real_16; /* Try and mimic HAVE_GFC_REAL_16 in libgfortran. */
  4266. have_gfc_real_16 = gfc_validate_kind (BT_REAL, 16, true) != -1;
  4267. /* Keep the number of bytes in sync with kiss_size in
  4268. libgfortran/intrinsics/random.c. */
  4269. kiss_size = (have_gfc_real_16 ? 48 : 32) / gfc_default_integer_kind;
  4270. if (size != NULL)
  4271. {
  4272. if (size->expr_type != EXPR_VARIABLE
  4273. || !size->symtree->n.sym->attr.optional)
  4274. nargs++;
  4275. if (!scalar_check (size, 0))
  4276. return false;
  4277. if (!type_check (size, 0, BT_INTEGER))
  4278. return false;
  4279. if (!variable_check (size, 0, false))
  4280. return false;
  4281. if (!kind_value_check (size, 0, gfc_default_integer_kind))
  4282. return false;
  4283. }
  4284. if (put != NULL)
  4285. {
  4286. if (put->expr_type != EXPR_VARIABLE
  4287. || !put->symtree->n.sym->attr.optional)
  4288. {
  4289. nargs++;
  4290. where = &put->where;
  4291. }
  4292. if (!array_check (put, 1))
  4293. return false;
  4294. if (!rank_check (put, 1, 1))
  4295. return false;
  4296. if (!type_check (put, 1, BT_INTEGER))
  4297. return false;
  4298. if (!kind_value_check (put, 1, gfc_default_integer_kind))
  4299. return false;
  4300. if (gfc_array_size (put, &put_size)
  4301. && mpz_get_ui (put_size) < kiss_size)
  4302. gfc_error ("Size of %qs argument of %qs intrinsic at %L "
  4303. "too small (%i/%i)",
  4304. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  4305. where, (int) mpz_get_ui (put_size), kiss_size);
  4306. }
  4307. if (get != NULL)
  4308. {
  4309. if (get->expr_type != EXPR_VARIABLE
  4310. || !get->symtree->n.sym->attr.optional)
  4311. {
  4312. nargs++;
  4313. where = &get->where;
  4314. }
  4315. if (!array_check (get, 2))
  4316. return false;
  4317. if (!rank_check (get, 2, 1))
  4318. return false;
  4319. if (!type_check (get, 2, BT_INTEGER))
  4320. return false;
  4321. if (!variable_check (get, 2, false))
  4322. return false;
  4323. if (!kind_value_check (get, 2, gfc_default_integer_kind))
  4324. return false;
  4325. if (gfc_array_size (get, &get_size)
  4326. && mpz_get_ui (get_size) < kiss_size)
  4327. gfc_error ("Size of %qs argument of %qs intrinsic at %L "
  4328. "too small (%i/%i)",
  4329. gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
  4330. where, (int) mpz_get_ui (get_size), kiss_size);
  4331. }
  4332. /* RANDOM_SEED may not have more than one non-optional argument. */
  4333. if (nargs > 1)
  4334. gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
  4335. return true;
  4336. }
  4337. bool
  4338. gfc_check_second_sub (gfc_expr *time)
  4339. {
  4340. if (!scalar_check (time, 0))
  4341. return false;
  4342. if (!type_check (time, 0, BT_REAL))
  4343. return false;
  4344. if (!kind_value_check (time, 0, 4))
  4345. return false;
  4346. return true;
  4347. }
  4348. /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
  4349. variables in Fortran 95. In Fortran 2003 and later, they can be of any
  4350. kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
  4351. count_max are all optional arguments */
  4352. bool
  4353. gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
  4354. gfc_expr *count_max)
  4355. {
  4356. if (count != NULL)
  4357. {
  4358. if (!scalar_check (count, 0))
  4359. return false;
  4360. if (!type_check (count, 0, BT_INTEGER))
  4361. return false;
  4362. if (count->ts.kind != gfc_default_integer_kind
  4363. && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
  4364. "SYSTEM_CLOCK at %L has non-default kind",
  4365. &count->where))
  4366. return false;
  4367. if (!variable_check (count, 0, false))
  4368. return false;
  4369. }
  4370. if (count_rate != NULL)
  4371. {
  4372. if (!scalar_check (count_rate, 1))
  4373. return false;
  4374. if (!variable_check (count_rate, 1, false))
  4375. return false;
  4376. if (count_rate->ts.type == BT_REAL)
  4377. {
  4378. if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
  4379. "SYSTEM_CLOCK at %L", &count_rate->where))
  4380. return false;
  4381. }
  4382. else
  4383. {
  4384. if (!type_check (count_rate, 1, BT_INTEGER))
  4385. return false;
  4386. if (count_rate->ts.kind != gfc_default_integer_kind
  4387. && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
  4388. "SYSTEM_CLOCK at %L has non-default kind",
  4389. &count_rate->where))
  4390. return false;
  4391. }
  4392. }
  4393. if (count_max != NULL)
  4394. {
  4395. if (!scalar_check (count_max, 2))
  4396. return false;
  4397. if (!type_check (count_max, 2, BT_INTEGER))
  4398. return false;
  4399. if (count_max->ts.kind != gfc_default_integer_kind
  4400. && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
  4401. "SYSTEM_CLOCK at %L has non-default kind",
  4402. &count_max->where))
  4403. return false;
  4404. if (!variable_check (count_max, 2, false))
  4405. return false;
  4406. }
  4407. return true;
  4408. }
  4409. bool
  4410. gfc_check_irand (gfc_expr *x)
  4411. {
  4412. if (x == NULL)
  4413. return true;
  4414. if (!scalar_check (x, 0))
  4415. return false;
  4416. if (!type_check (x, 0, BT_INTEGER))
  4417. return false;
  4418. if (!kind_value_check (x, 0, 4))
  4419. return false;
  4420. return true;
  4421. }
  4422. bool
  4423. gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
  4424. {
  4425. if (!scalar_check (seconds, 0))
  4426. return false;
  4427. if (!type_check (seconds, 0, BT_INTEGER))
  4428. return false;
  4429. if (!int_or_proc_check (handler, 1))
  4430. return false;
  4431. if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
  4432. return false;
  4433. if (status == NULL)
  4434. return true;
  4435. if (!scalar_check (status, 2))
  4436. return false;
  4437. if (!type_check (status, 2, BT_INTEGER))
  4438. return false;
  4439. if (!kind_value_check (status, 2, gfc_default_integer_kind))
  4440. return false;
  4441. return true;
  4442. }
  4443. bool
  4444. gfc_check_rand (gfc_expr *x)
  4445. {
  4446. if (x == NULL)
  4447. return true;
  4448. if (!scalar_check (x, 0))
  4449. return false;
  4450. if (!type_check (x, 0, BT_INTEGER))
  4451. return false;
  4452. if (!kind_value_check (x, 0, 4))
  4453. return false;
  4454. return true;
  4455. }
  4456. bool
  4457. gfc_check_srand (gfc_expr *x)
  4458. {
  4459. if (!scalar_check (x, 0))
  4460. return false;
  4461. if (!type_check (x, 0, BT_INTEGER))
  4462. return false;
  4463. if (!kind_value_check (x, 0, 4))
  4464. return false;
  4465. return true;
  4466. }
  4467. bool
  4468. gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
  4469. {
  4470. if (!scalar_check (time, 0))
  4471. return false;
  4472. if (!type_check (time, 0, BT_INTEGER))
  4473. return false;
  4474. if (!type_check (result, 1, BT_CHARACTER))
  4475. return false;
  4476. if (!kind_value_check (result, 1, gfc_default_character_kind))
  4477. return false;
  4478. return true;
  4479. }
  4480. bool
  4481. gfc_check_dtime_etime (gfc_expr *x)
  4482. {
  4483. if (!array_check (x, 0))
  4484. return false;
  4485. if (!rank_check (x, 0, 1))
  4486. return false;
  4487. if (!variable_check (x, 0, false))
  4488. return false;
  4489. if (!type_check (x, 0, BT_REAL))
  4490. return false;
  4491. if (!kind_value_check (x, 0, 4))
  4492. return false;
  4493. return true;
  4494. }
  4495. bool
  4496. gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
  4497. {
  4498. if (!array_check (values, 0))
  4499. return false;
  4500. if (!rank_check (values, 0, 1))
  4501. return false;
  4502. if (!variable_check (values, 0, false))
  4503. return false;
  4504. if (!type_check (values, 0, BT_REAL))
  4505. return false;
  4506. if (!kind_value_check (values, 0, 4))
  4507. return false;
  4508. if (!scalar_check (time, 1))
  4509. return false;
  4510. if (!type_check (time, 1, BT_REAL))
  4511. return false;
  4512. if (!kind_value_check (time, 1, 4))
  4513. return false;
  4514. return true;
  4515. }
  4516. bool
  4517. gfc_check_fdate_sub (gfc_expr *date)
  4518. {
  4519. if (!type_check (date, 0, BT_CHARACTER))
  4520. return false;
  4521. if (!kind_value_check (date, 0, gfc_default_character_kind))
  4522. return false;
  4523. return true;
  4524. }
  4525. bool
  4526. gfc_check_gerror (gfc_expr *msg)
  4527. {
  4528. if (!type_check (msg, 0, BT_CHARACTER))
  4529. return false;
  4530. if (!kind_value_check (msg, 0, gfc_default_character_kind))
  4531. return false;
  4532. return true;
  4533. }
  4534. bool
  4535. gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
  4536. {
  4537. if (!type_check (cwd, 0, BT_CHARACTER))
  4538. return false;
  4539. if (!kind_value_check (cwd, 0, gfc_default_character_kind))
  4540. return false;
  4541. if (status == NULL)
  4542. return true;
  4543. if (!scalar_check (status, 1))
  4544. return false;
  4545. if (!type_check (status, 1, BT_INTEGER))
  4546. return false;
  4547. return true;
  4548. }
  4549. bool
  4550. gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
  4551. {
  4552. if (!type_check (pos, 0, BT_INTEGER))
  4553. return false;
  4554. if (pos->ts.kind > gfc_default_integer_kind)
  4555. {
  4556. gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
  4557. "not wider than the default kind (%d)",
  4558. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  4559. &pos->where, gfc_default_integer_kind);
  4560. return false;
  4561. }
  4562. if (!type_check (value, 1, BT_CHARACTER))
  4563. return false;
  4564. if (!kind_value_check (value, 1, gfc_default_character_kind))
  4565. return false;
  4566. return true;
  4567. }
  4568. bool
  4569. gfc_check_getlog (gfc_expr *msg)
  4570. {
  4571. if (!type_check (msg, 0, BT_CHARACTER))
  4572. return false;
  4573. if (!kind_value_check (msg, 0, gfc_default_character_kind))
  4574. return false;
  4575. return true;
  4576. }
  4577. bool
  4578. gfc_check_exit (gfc_expr *status)
  4579. {
  4580. if (status == NULL)
  4581. return true;
  4582. if (!type_check (status, 0, BT_INTEGER))
  4583. return false;
  4584. if (!scalar_check (status, 0))
  4585. return false;
  4586. return true;
  4587. }
  4588. bool
  4589. gfc_check_flush (gfc_expr *unit)
  4590. {
  4591. if (unit == NULL)
  4592. return true;
  4593. if (!type_check (unit, 0, BT_INTEGER))
  4594. return false;
  4595. if (!scalar_check (unit, 0))
  4596. return false;
  4597. return true;
  4598. }
  4599. bool
  4600. gfc_check_free (gfc_expr *i)
  4601. {
  4602. if (!type_check (i, 0, BT_INTEGER))
  4603. return false;
  4604. if (!scalar_check (i, 0))
  4605. return false;
  4606. return true;
  4607. }
  4608. bool
  4609. gfc_check_hostnm (gfc_expr *name)
  4610. {
  4611. if (!type_check (name, 0, BT_CHARACTER))
  4612. return false;
  4613. if (!kind_value_check (name, 0, gfc_default_character_kind))
  4614. return false;
  4615. return true;
  4616. }
  4617. bool
  4618. gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
  4619. {
  4620. if (!type_check (name, 0, BT_CHARACTER))
  4621. return false;
  4622. if (!kind_value_check (name, 0, gfc_default_character_kind))
  4623. return false;
  4624. if (status == NULL)
  4625. return true;
  4626. if (!scalar_check (status, 1))
  4627. return false;
  4628. if (!type_check (status, 1, BT_INTEGER))
  4629. return false;
  4630. return true;
  4631. }
  4632. bool
  4633. gfc_check_itime_idate (gfc_expr *values)
  4634. {
  4635. if (!array_check (values, 0))
  4636. return false;
  4637. if (!rank_check (values, 0, 1))
  4638. return false;
  4639. if (!variable_check (values, 0, false))
  4640. return false;
  4641. if (!type_check (values, 0, BT_INTEGER))
  4642. return false;
  4643. if (!kind_value_check (values, 0, gfc_default_integer_kind))
  4644. return false;
  4645. return true;
  4646. }
  4647. bool
  4648. gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
  4649. {
  4650. if (!type_check (time, 0, BT_INTEGER))
  4651. return false;
  4652. if (!kind_value_check (time, 0, gfc_default_integer_kind))
  4653. return false;
  4654. if (!scalar_check (time, 0))
  4655. return false;
  4656. if (!array_check (values, 1))
  4657. return false;
  4658. if (!rank_check (values, 1, 1))
  4659. return false;
  4660. if (!variable_check (values, 1, false))
  4661. return false;
  4662. if (!type_check (values, 1, BT_INTEGER))
  4663. return false;
  4664. if (!kind_value_check (values, 1, gfc_default_integer_kind))
  4665. return false;
  4666. return true;
  4667. }
  4668. bool
  4669. gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
  4670. {
  4671. if (!scalar_check (unit, 0))
  4672. return false;
  4673. if (!type_check (unit, 0, BT_INTEGER))
  4674. return false;
  4675. if (!type_check (name, 1, BT_CHARACTER))
  4676. return false;
  4677. if (!kind_value_check (name, 1, gfc_default_character_kind))
  4678. return false;
  4679. return true;
  4680. }
  4681. bool
  4682. gfc_check_isatty (gfc_expr *unit)
  4683. {
  4684. if (unit == NULL)
  4685. return false;
  4686. if (!type_check (unit, 0, BT_INTEGER))
  4687. return false;
  4688. if (!scalar_check (unit, 0))
  4689. return false;
  4690. return true;
  4691. }
  4692. bool
  4693. gfc_check_isnan (gfc_expr *x)
  4694. {
  4695. if (!type_check (x, 0, BT_REAL))
  4696. return false;
  4697. return true;
  4698. }
  4699. bool
  4700. gfc_check_perror (gfc_expr *string)
  4701. {
  4702. if (!type_check (string, 0, BT_CHARACTER))
  4703. return false;
  4704. if (!kind_value_check (string, 0, gfc_default_character_kind))
  4705. return false;
  4706. return true;
  4707. }
  4708. bool
  4709. gfc_check_umask (gfc_expr *mask)
  4710. {
  4711. if (!type_check (mask, 0, BT_INTEGER))
  4712. return false;
  4713. if (!scalar_check (mask, 0))
  4714. return false;
  4715. return true;
  4716. }
  4717. bool
  4718. gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
  4719. {
  4720. if (!type_check (mask, 0, BT_INTEGER))
  4721. return false;
  4722. if (!scalar_check (mask, 0))
  4723. return false;
  4724. if (old == NULL)
  4725. return true;
  4726. if (!scalar_check (old, 1))
  4727. return false;
  4728. if (!type_check (old, 1, BT_INTEGER))
  4729. return false;
  4730. return true;
  4731. }
  4732. bool
  4733. gfc_check_unlink (gfc_expr *name)
  4734. {
  4735. if (!type_check (name, 0, BT_CHARACTER))
  4736. return false;
  4737. if (!kind_value_check (name, 0, gfc_default_character_kind))
  4738. return false;
  4739. return true;
  4740. }
  4741. bool
  4742. gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
  4743. {
  4744. if (!type_check (name, 0, BT_CHARACTER))
  4745. return false;
  4746. if (!kind_value_check (name, 0, gfc_default_character_kind))
  4747. return false;
  4748. if (status == NULL)
  4749. return true;
  4750. if (!scalar_check (status, 1))
  4751. return false;
  4752. if (!type_check (status, 1, BT_INTEGER))
  4753. return false;
  4754. return true;
  4755. }
  4756. bool
  4757. gfc_check_signal (gfc_expr *number, gfc_expr *handler)
  4758. {
  4759. if (!scalar_check (number, 0))
  4760. return false;
  4761. if (!type_check (number, 0, BT_INTEGER))
  4762. return false;
  4763. if (!int_or_proc_check (handler, 1))
  4764. return false;
  4765. if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
  4766. return false;
  4767. return true;
  4768. }
  4769. bool
  4770. gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
  4771. {
  4772. if (!scalar_check (number, 0))
  4773. return false;
  4774. if (!type_check (number, 0, BT_INTEGER))
  4775. return false;
  4776. if (!int_or_proc_check (handler, 1))
  4777. return false;
  4778. if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
  4779. return false;
  4780. if (status == NULL)
  4781. return true;
  4782. if (!type_check (status, 2, BT_INTEGER))
  4783. return false;
  4784. if (!scalar_check (status, 2))
  4785. return false;
  4786. return true;
  4787. }
  4788. bool
  4789. gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
  4790. {
  4791. if (!type_check (cmd, 0, BT_CHARACTER))
  4792. return false;
  4793. if (!kind_value_check (cmd, 0, gfc_default_character_kind))
  4794. return false;
  4795. if (!scalar_check (status, 1))
  4796. return false;
  4797. if (!type_check (status, 1, BT_INTEGER))
  4798. return false;
  4799. if (!kind_value_check (status, 1, gfc_default_integer_kind))
  4800. return false;
  4801. return true;
  4802. }
  4803. /* This is used for the GNU intrinsics AND, OR and XOR. */
  4804. bool
  4805. gfc_check_and (gfc_expr *i, gfc_expr *j)
  4806. {
  4807. if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
  4808. {
  4809. gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
  4810. "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
  4811. gfc_current_intrinsic, &i->where);
  4812. return false;
  4813. }
  4814. if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
  4815. {
  4816. gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
  4817. "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
  4818. gfc_current_intrinsic, &j->where);
  4819. return false;
  4820. }
  4821. if (i->ts.type != j->ts.type)
  4822. {
  4823. gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
  4824. "have the same type", gfc_current_intrinsic_arg[0]->name,
  4825. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  4826. &j->where);
  4827. return false;
  4828. }
  4829. if (!scalar_check (i, 0))
  4830. return false;
  4831. if (!scalar_check (j, 1))
  4832. return false;
  4833. return true;
  4834. }
  4835. bool
  4836. gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
  4837. {
  4838. if (a->expr_type == EXPR_NULL)
  4839. {
  4840. gfc_error ("Intrinsic function NULL at %L cannot be an actual "
  4841. "argument to STORAGE_SIZE, because it returns a "
  4842. "disassociated pointer", &a->where);
  4843. return false;
  4844. }
  4845. if (a->ts.type == BT_ASSUMED)
  4846. {
  4847. gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
  4848. gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
  4849. &a->where);
  4850. return false;
  4851. }
  4852. if (a->ts.type == BT_PROCEDURE)
  4853. {
  4854. gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
  4855. "procedure", gfc_current_intrinsic_arg[0]->name,
  4856. gfc_current_intrinsic, &a->where);
  4857. return false;
  4858. }
  4859. if (kind == NULL)
  4860. return true;
  4861. if (!type_check (kind, 1, BT_INTEGER))
  4862. return false;
  4863. if (!scalar_check (kind, 1))
  4864. return false;
  4865. if (kind->expr_type != EXPR_CONSTANT)
  4866. {
  4867. gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
  4868. gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
  4869. &kind->where);
  4870. return false;
  4871. }
  4872. return true;
  4873. }