ncal.pas 242 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750
  1. {
  2. This file implements the node for sub procedure calling.
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncal;
  18. {$i fpcdefs.inc}
  19. { $define DEBUGINLINE}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,constexp,
  24. paramgr,parabase,cgbase,
  25. node,nbas,nutils,
  26. {$ifdef state_tracking}
  27. nstate,
  28. {$endif state_tracking}
  29. symbase,symtype,symsym,symdef,symtable,
  30. pgentype,compinnr;
  31. type
  32. tcallnodeflag = (
  33. cnf_typedefset,
  34. cnf_return_value_used,
  35. cnf_do_inline,
  36. cnf_inherited,
  37. cnf_anon_inherited,
  38. cnf_new_call,
  39. cnf_dispose_call,
  40. cnf_member_call, { called with implicit methodpointer tree }
  41. cnf_uses_varargs, { varargs are used in the declaration }
  42. cnf_create_failed, { exception thrown in constructor -> don't call beforedestruction }
  43. cnf_objc_processed, { the procedure name has been set to the appropriate objc_msgSend* variant -> don't process again }
  44. cnf_objc_id_call, { the procedure is a member call via id -> any ObjC method of any ObjC type in scope is fair game }
  45. cnf_unit_specified, { the unit in which the procedure has to be searched has been specified }
  46. cnf_call_never_returns, { information for the dfa that a subroutine never returns }
  47. cnf_call_self_node_done,{ the call_self_node has been generated if necessary
  48. (to prevent it from potentially happening again in a wrong context in case of constant propagation or so) }
  49. cnf_ignore_visibility, { internally generated call that should ignore visibility checks }
  50. cnf_check_fpu_exceptions, { after the call fpu exceptions shall be checked }
  51. cnf_ignore_devirt_wpo, { ignore this call for devirtualisation info tracking: calls to newinstance generated by the compiler do not result in extra class types being instanced }
  52. cnf_no_convert_procvar { don't convert a procdef to a procvar }
  53. );
  54. tcallnodeflags = set of tcallnodeflag;
  55. tcallparanode = class;
  56. tcallnode = class(tbinarynode)
  57. private
  58. { number of parameters passed from the source, this does not include the hidden parameters }
  59. paralength : smallint;
  60. function getoverrideprocnamedef: tprocdef; inline;
  61. function is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  62. procedure maybe_load_in_temp(var p:tnode);
  63. function gen_high_tree(var p:tnode;paradef:tdef):tnode;
  64. function gen_procvar_context_tree_self:tnode;
  65. function gen_procvar_context_tree_parentfp:tnode;
  66. function gen_self_tree:tnode;
  67. function use_caller_self(check_for_callee_self: boolean): boolean;
  68. procedure maybe_gen_call_self_node;
  69. function gen_vmt_tree:tnode;
  70. function gen_block_context:tnode;
  71. procedure gen_hidden_parameters;
  72. function funcret_can_be_reused:boolean;
  73. procedure maybe_create_funcret_node;
  74. procedure bind_parasym;
  75. procedure add_init_statement(n:tnode);
  76. procedure add_done_statement(n:tnode);
  77. procedure convert_carg_array_of_const;
  78. procedure order_parameters;
  79. function heuristics_favors_inlining:boolean;
  80. procedure check_inlining;
  81. function pass1_normal:tnode;
  82. procedure register_created_object_types;
  83. function get_expect_loc: tcgloc;
  84. function handle_compilerproc: tnode;
  85. protected
  86. function safe_call_self_node: tnode;
  87. procedure load_in_temp(var p:tnode);
  88. procedure gen_vmt_entry_load; virtual;
  89. procedure gen_syscall_para(para: tcallparanode); virtual;
  90. procedure objc_convert_to_message_send;virtual;
  91. protected
  92. { inlining support }
  93. inlinelocals : TFPObjectList;
  94. inlineinitstatement,
  95. inlinecleanupstatement : tstatementnode;
  96. { checks whether we have to create a temp to store the value of a
  97. parameter passed to an inline routine to preserve correctness.
  98. On exit, complexpara contains true if the parameter is a complex
  99. expression and for which we can try to create a temp (even though
  100. it's not strictly necessary) for speed and code size reasons.
  101. Returns true if the temp creation has been handled, false otherwise
  102. }
  103. function paraneedsinlinetemp(para: tcallparanode; const pushconstaddr, complexpara: boolean): boolean; virtual;
  104. function maybecreateinlineparatemp(para: tcallparanode; out complexpara: boolean): boolean;
  105. procedure createinlineparas;
  106. procedure wrapcomplexinlinepara(para: tcallparanode); virtual;
  107. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  108. procedure createlocaltemps(p:TObject;arg:pointer);
  109. function optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  110. function pass1_inline:tnode;
  111. protected
  112. pushedparasize : longint;
  113. { Objective-C support: force the call node to call the routine with
  114. this name rather than the name of symtableprocentry (don't store
  115. to ppu, is set while processing the node). Also used on the JVM
  116. target for calling virtual methods, as this is name-based and not
  117. based on VMT entry locations }
  118. foverrideprocnamedef: tprocdef;
  119. property overrideprocnamedef: tprocdef read getoverrideprocnamedef;
  120. public
  121. { the symbol containing the definition of the procedure }
  122. { to call }
  123. symtableprocentry : tprocsym;
  124. symtableprocentryderef : tderef;
  125. { symtable where the entry was found, needed for with support }
  126. symtableproc : TSymtable;
  127. { the definition of the procedure to call }
  128. procdefinition : tabstractprocdef;
  129. procdefinitionderef : tderef;
  130. { tree that contains the pointer to the object for this method }
  131. methodpointer : tnode;
  132. { tree representing the VMT entry to call (if any) }
  133. vmt_entry : tnode;
  134. { tree that contains the self/vmt parameter when this node was created
  135. (so it's still valid when this node is processed in an inline
  136. context)
  137. }
  138. call_self_node,
  139. call_vmt_node: tnode;
  140. { initialize/finalization of temps }
  141. callinitblock,
  142. callcleanupblock : tblocknode;
  143. { function return node for initialized types or supplied return variable.
  144. When the result is passed in a parameter then it is set to nil }
  145. funcretnode : tnode;
  146. { varargs parasyms }
  147. varargsparas : tvarargsparalist;
  148. { If an inline node is transmuted into a call node, this is the index of
  149. the original internal routine }
  150. intrinsiccode : TInlineNumber;
  151. { separately specified resultdef for some compilerprocs (e.g.
  152. you can't have a function with an "array of char" resultdef
  153. the RTL) (JM)
  154. }
  155. typedef: tdef;
  156. callnodeflags : tcallnodeflags;
  157. spezcontext : tspecializationcontext;
  158. { only the processor specific nodes need to override this }
  159. { constructor }
  160. constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags;sc:tspecializationcontext);virtual;
  161. constructor create_procvar(l,r:tnode);
  162. constructor createintern(const name: string; params: tnode);
  163. constructor createfromintrinsic(const intrinsic: TInlineNumber; const name: string; params: tnode);
  164. constructor createinternfromunit(const fromunit, procname: string; params: tnode);
  165. constructor createinternres(const name: string; params: tnode; res:tdef);
  166. constructor createinternresfromunit(const fromunit, procname: string; params: tnode; res:tdef);
  167. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  168. constructor createinternmethod(mp: tnode; const name: string; params: tnode);
  169. constructor createinternmethodres(mp: tnode; const name: string; params: tnode; res:tdef);
  170. destructor destroy;override;
  171. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  172. procedure ppuwrite(ppufile:tcompilerppufile);override;
  173. procedure buildderefimpl;override;
  174. procedure derefimpl;override;
  175. function dogetcopy : tnode;override;
  176. { Goes through all symbols in a class and subclasses and calls
  177. verify abstract for each .
  178. }
  179. procedure verifyabstractcalls;
  180. { called for each definition in a class and verifies if a method
  181. is abstract or not, if it is abstract, give out a warning
  182. }
  183. procedure verifyabstract(sym:TObject;arg:pointer);
  184. procedure insertintolist(l : tnodelist);override;
  185. function pass_1 : tnode;override;
  186. function pass_typecheck:tnode;override;
  187. function simplify(forinline : boolean) : tnode;override;
  188. {$ifdef state_tracking}
  189. function track_state_pass(exec_known:boolean):boolean;override;
  190. {$endif state_tracking}
  191. function docompare(p: tnode): boolean; override;
  192. procedure printnodedata(var t:text);override;
  193. {$ifdef DEBUG_NODE_XML}
  194. procedure XMLPrintNodeData(var T: Text); override;
  195. {$endif DEBUG_NODE_XML}
  196. function para_count:longint;
  197. function required_para_count:longint;
  198. function GetParaFromIndex(const Index: Integer): TCallParaNode;
  199. { checks if there are any parameters which end up at the stack, i.e.
  200. which have LOC_REFERENCE and set pi_has_stackparameter if this applies }
  201. procedure check_stack_parameters;
  202. { force the name of the to-be-called routine to a particular string,
  203. used for Objective-C message sending. }
  204. property parameters : tnode read left write left;
  205. property pushed_parasize: longint read pushedparasize;
  206. private
  207. AbstractMethodsList : TFPHashList;
  208. end;
  209. tcallnodeclass = class of tcallnode;
  210. tcallparaflag = (
  211. cpf_is_colon_para,
  212. cpf_varargs_para { belongs this para to varargs }
  213. );
  214. tcallparaflags = set of tcallparaflag;
  215. tcallparanode = class(ttertiarynode)
  216. private
  217. fcontains_stack_tainting_call_cached,
  218. ffollowed_by_stack_tainting_call_cached : boolean;
  219. protected
  220. procedure handlemanagedbyrefpara(orgparadef: tdef);virtual;
  221. { on some targets, value parameters that are passed by reference must
  222. be copied to a temp location by the caller (and then a reference to
  223. this temp location must be passed) }
  224. procedure copy_value_by_ref_para;
  225. public
  226. { in case of copy-out parameters: initialization code, and the code to
  227. copy back the parameter value after the call (including any required
  228. finalization code) }
  229. fparainit,
  230. fparacopyback: tnode;
  231. callparaflags : tcallparaflags;
  232. parasym : tparavarsym;
  233. { The original order of the parameters prior to the "order_parameters"
  234. call, or -1 if not yet configured }
  235. originalindex: Integer;
  236. { only the processor specific nodes need to override this }
  237. { constructor }
  238. constructor create(expr,next : tnode);virtual;
  239. destructor destroy;override;
  240. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  241. procedure ppuwrite(ppufile:tcompilerppufile);override;
  242. procedure buildderefimpl; override;
  243. procedure derefimpl; override;
  244. function dogetcopy : tnode;override;
  245. procedure insertintolist(l : tnodelist);override;
  246. function pass_typecheck : tnode;override;
  247. function pass_1 : tnode;override;
  248. procedure get_paratype;
  249. procedure firstcallparan;
  250. procedure insert_typeconv;
  251. procedure secondcallparan;virtual;abstract;
  252. function docompare(p: tnode): boolean; override;
  253. procedure printnodetree(var t:text);override;
  254. { returns whether a parameter contains a type conversion from }
  255. { a refcounted into a non-refcounted type }
  256. function can_be_inlined: boolean;
  257. property paravalue : tnode read left write left;
  258. property nextpara : tnode read right write right;
  259. { third is reused to store the parameter name (only while parsing
  260. vardispatch calls, never in real node tree) and copy of 'high'
  261. parameter tree when the parameter is an open array of managed type }
  262. property parametername : tnode read third write third;
  263. { returns whether the evaluation of this parameter involves a
  264. stack tainting call }
  265. function contains_stack_tainting_call: boolean;
  266. { initialises the fcontains_stack_tainting_call_cached field with the
  267. result of contains_stack_tainting_call so that it can be quickly
  268. accessed via the contains_stack_tainting_call_cached property }
  269. procedure init_contains_stack_tainting_call_cache;
  270. { returns result of contains_stack_tainting_call cached during last
  271. call to init_contains_stack_tainting_call_cache }
  272. property contains_stack_tainting_call_cached: boolean read fcontains_stack_tainting_call_cached;
  273. { returns whether this parameter is followed by at least one other
  274. parameter whose evaluation involves a stack tainting parameter
  275. (result is only valid after order_parameters has been called) }
  276. property followed_by_stack_tainting_call_cached: boolean read ffollowed_by_stack_tainting_call_cached;
  277. property paracopyback: tnode read fparacopyback;
  278. end;
  279. tcallparanodeclass = class of tcallparanode;
  280. tdispcalltype = (
  281. dct_method,
  282. dct_propget,
  283. dct_propput
  284. );
  285. { also returns the number of parameters }
  286. function reverseparameters(var p: tcallparanode) : sizeint;
  287. function translate_disp_call(selfnode,parametersnode: tnode; calltype: tdispcalltype; const methodname : ansistring;
  288. dispid : longint;resultdef : tdef) : tnode;
  289. var
  290. ccallnode : tcallnodeclass = tcallnode;
  291. ccallparanode : tcallparanodeclass = tcallparanode;
  292. { Current callnode, this is needed for having a link
  293. between the callparanodes and the callnode they belong to }
  294. aktcallnode : tcallnode;
  295. const
  296. { track current inlining depth }
  297. inlinelevel : longint = 0;
  298. implementation
  299. uses
  300. systems,
  301. verbose,globals,fmodule,ppu,
  302. aasmbase,aasmdata,
  303. symconst,defutil,defcmp,
  304. htypechk,pass_1,
  305. ncnv,nflw,nld,ninl,nadd,ncon,nmem,nset,nobjc,
  306. pgenutil,
  307. ngenutil,objcutil,aasmcnst,
  308. procinfo,cpuinfo,
  309. wpobase;
  310. type
  311. tobjectinfoitem = class(tlinkedlistitem)
  312. objinfo : tobjectdef;
  313. constructor create(def : tobjectdef);
  314. end;
  315. {****************************************************************************
  316. HELPERS
  317. ****************************************************************************}
  318. function reverseparameters(var p: tcallparanode) : sizeint;
  319. var
  320. tmpp,
  321. hp1, hp2: tcallparanode;
  322. begin
  323. result:=0;
  324. hp1:=nil;
  325. tmpp:=p;
  326. while assigned(tmpp) do
  327. begin
  328. { pull out }
  329. hp2:=tmpp;
  330. tmpp:=tcallparanode(tmpp.right);
  331. { pull in }
  332. hp2.right:=hp1;
  333. hp1:=hp2;
  334. inc(result);
  335. end;
  336. p:=hp1;
  337. end;
  338. function translate_disp_call(selfnode,parametersnode: tnode; calltype: tdispcalltype; const methodname : ansistring;
  339. dispid : longint;resultdef : tdef) : tnode;
  340. const
  341. DISPATCH_METHOD = $1;
  342. DISPATCH_PROPERTYGET = $2;
  343. DISPATCH_PROPERTYPUT = $4;
  344. DISPATCH_PROPERTYPUTREF = $8;
  345. DISPATCH_CONSTRUCT = $4000;
  346. calltypes: array[tdispcalltype] of byte = (
  347. DISPATCH_METHOD, DISPATCH_PROPERTYGET, DISPATCH_PROPERTYPUT
  348. );
  349. var
  350. statements : tstatementnode;
  351. result_data,
  352. params : ttempcreatenode;
  353. paramssize : cardinal;
  354. resultvalue : tnode;
  355. para : tcallparanode;
  356. namedparacount,
  357. paracount : longint;
  358. assignmenttype,
  359. vardatadef,
  360. pvardatadef : tdef;
  361. useresult: boolean;
  362. restype: byte;
  363. selftemp: ttempcreatenode;
  364. selfpara: tnode;
  365. vardispatchparadef: trecorddef;
  366. vardispatchfield: tsym;
  367. tcb: ttai_typedconstbuilder;
  368. calldescsym: tstaticvarsym;
  369. names : ansistring;
  370. variantdispatch : boolean;
  371. function is_byref_para(out assign_type: tdef): boolean;
  372. begin
  373. result:=(assigned(para.parasym) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) or
  374. (variantdispatch and valid_for_var(para.left,false));
  375. if result or (para.left.resultdef.typ in [variantdef]) then
  376. assign_type:=voidpointertype
  377. else
  378. case para.left.resultdef.size of
  379. 1..4:
  380. assign_type:=u32inttype;
  381. 8:
  382. assign_type:=u64inttype;
  383. else
  384. internalerror(2007042801);
  385. end;
  386. end;
  387. function getvardef(sourcedef: TDef): longint;
  388. begin
  389. if is_ansistring(sourcedef) then
  390. result:=varStrArg
  391. else
  392. if is_unicodestring(sourcedef) then
  393. result:=varUStrArg
  394. else
  395. if is_interfacecom_or_dispinterface(sourcedef) then
  396. begin
  397. { distinct IDispatch and IUnknown interfaces }
  398. if def_is_related(tobjectdef(sourcedef),interface_idispatch) then
  399. result:=vardispatch
  400. else
  401. result:=varunknown;
  402. end
  403. else
  404. result:=sourcedef.getvardef;
  405. end;
  406. begin
  407. variantdispatch:=selfnode.resultdef.typ=variantdef;
  408. result:=internalstatements(statements);
  409. result_data:=nil;
  410. selftemp:=nil;
  411. selfpara:=nil;
  412. useresult := assigned(resultdef) and not is_void(resultdef);
  413. if useresult then
  414. begin
  415. { get temp for the result }
  416. result_data:=ctempcreatenode.create(colevarianttype,colevarianttype.size,tt_persistent,true);
  417. addstatement(statements,result_data);
  418. end;
  419. { first, count and check parameters }
  420. para:=tcallparanode(parametersnode);
  421. paracount:=0;
  422. namedparacount:=0;
  423. while assigned(para) do
  424. begin
  425. typecheckpass(para.left);
  426. { skip hidden dispinterface parameters like $self, $result,
  427. but count skipped variantdispatch parameters. }
  428. if (not variantdispatch) and (para.left.nodetype=nothingn) then
  429. begin
  430. para:=tcallparanode(para.nextpara);
  431. continue;
  432. end;
  433. inc(paracount);
  434. if assigned(para.parametername) then
  435. inc(namedparacount);
  436. { insert some extra casts }
  437. if para.left.nodetype=stringconstn then
  438. inserttypeconv_internal(para.left,cwidestringtype)
  439. { force automatable boolean type }
  440. else if is_boolean(para.left.resultdef) then
  441. inserttypeconv_internal(para.left,bool16type)
  442. { force automatable float type }
  443. else if is_extended(para.left.resultdef)
  444. and (current_settings.fputype<>fpu_none) then
  445. inserttypeconv_internal(para.left,s64floattype)
  446. else if is_shortstring(para.left.resultdef) then
  447. inserttypeconv_internal(para.left,cwidestringtype)
  448. { skip this check if we've already typecasted to automatable type }
  449. else if (para.left.nodetype<>nothingn) and (not is_automatable(para.left.resultdef)) then
  450. CGMessagePos1(para.left.fileinfo,type_e_not_automatable,para.left.resultdef.typename);
  451. para:=tcallparanode(para.nextpara);
  452. end;
  453. { create a temp to store parameter values }
  454. vardispatchparadef:=crecorddef.create_global_internal('',voidpointertype.size,voidpointertype.size);
  455. { the size will be set once the vardistpatchparadef record has been completed }
  456. params:=ctempcreatenode.create(vardispatchparadef,0,tt_persistent,false);
  457. addstatement(statements,params);
  458. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  459. tcb.begin_anonymous_record('',1,sizeof(pint),1);
  460. if not variantdispatch then { generate a tdispdesc record }
  461. begin
  462. { dispid }
  463. tcb.emit_ord_const(dispid,s32inttype);
  464. { restype }
  465. if useresult then
  466. restype:=getvardef(resultdef)
  467. else
  468. restype:=0;
  469. tcb.emit_ord_const(restype,u8inttype);
  470. end;
  471. tcb.emit_ord_const(calltypes[calltype],u8inttype);
  472. tcb.emit_ord_const(paracount,u8inttype);
  473. tcb.emit_ord_const(namedparacount,u8inttype);
  474. { build up parameters and description }
  475. para:=tcallparanode(parametersnode);
  476. paramssize:=0;
  477. names := '';
  478. while assigned(para) do
  479. begin
  480. { Skipped parameters are actually (varType=varError, vError=DISP_E_PARAMNOTFOUND).
  481. Generate only varType here, the value will be added by RTL. }
  482. if para.left.nodetype=nothingn then
  483. begin
  484. if variantdispatch then
  485. tcb.emit_ord_const(varError,u8inttype);
  486. para:=tcallparanode(para.nextpara);
  487. continue;
  488. end;
  489. if assigned(para.parametername) then
  490. begin
  491. if para.parametername.nodetype=stringconstn then
  492. names:=names+tstringconstnode(para.parametername).asconstpchar+#0
  493. else
  494. internalerror(200611041);
  495. end;
  496. restype:=getvardef(para.left.resultdef);
  497. if is_byref_para(assignmenttype) then
  498. restype:=restype or $80;
  499. { assign the argument/parameter to the temporary location }
  500. { for Variants, we always pass a pointer, RTL helpers must handle it
  501. depending on byref bit }
  502. vardispatchfield:=vardispatchparadef.add_field_by_def('',assignmenttype);
  503. if assignmenttype=voidpointertype then
  504. addstatement(statements,cassignmentnode.create(
  505. csubscriptnode.create(vardispatchfield,ctemprefnode.create(params)),
  506. ctypeconvnode.create_internal(caddrnode.create_internal(para.left),voidpointertype)))
  507. else
  508. addstatement(statements,cassignmentnode.create(
  509. csubscriptnode.create(vardispatchfield,ctemprefnode.create(params)),
  510. ctypeconvnode.create_internal(para.left,assignmenttype)));
  511. inc(paramssize,max(voidpointertype.size,assignmenttype.size));
  512. tcb.emit_ord_const(restype,u8inttype);
  513. para.left:=nil;
  514. para:=tcallparanode(para.nextpara);
  515. end;
  516. { finalize the parameter record }
  517. trecordsymtable(vardispatchparadef.symtable).addalignmentpadding;
  518. { Set final size for parameter block }
  519. params.size:=paramssize;
  520. { old argument list skeleton isn't needed anymore }
  521. parametersnode.free;
  522. pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
  523. if useresult then
  524. resultvalue:=caddrnode.create(ctemprefnode.create(result_data))
  525. else
  526. resultvalue:=cpointerconstnode.create(0,voidpointertype);
  527. if variantdispatch then
  528. begin
  529. tcb.emit_pchar_const(pchar(methodname),length(methodname));
  530. if names<>'' then
  531. { length-1 because we added a null terminator to the string itself
  532. already }
  533. tcb.emit_pchar_const(pchar(names),length(names)-1);
  534. end;
  535. { may be referred from other units in case of inlining -> global
  536. -> must have unique name in entire progream }
  537. calldescsym:=cstaticvarsym.create(
  538. internaltypeprefixName[itp_vardisp_calldesc]+current_module.modulename^+'$'+tostr(current_module.localsymtable.SymList.count),
  539. vs_const,tcb.end_anonymous_record,[vo_is_public,vo_is_typed_const]);
  540. calldescsym.varstate:=vs_initialised;
  541. current_module.localsymtable.insertsym(calldescsym);
  542. current_asmdata.AsmLists[al_typedconsts].concatList(
  543. tcb.get_final_asmlist(
  544. current_asmdata.DefineAsmSymbol(calldescsym.mangledname,AB_GLOBAL,AT_DATA,calldescsym.vardef),
  545. calldescsym.vardef,sec_rodata_norel,
  546. lower(calldescsym.mangledname),sizeof(pint)
  547. )
  548. );
  549. tcb.free;
  550. if variantdispatch then
  551. begin
  552. { actual call }
  553. vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
  554. { the Variant should behave similar to hidden 'self' parameter of objects/records,
  555. see issues #26773 and #27044 }
  556. if not valid_for_var(selfnode,false) then
  557. begin
  558. selftemp:=ctempcreatenode.create(selfnode.resultdef,selfnode.resultdef.size,tt_persistent,false);
  559. addstatement(statements,selftemp);
  560. addstatement(statements,cassignmentnode.create(ctemprefnode.create(selftemp),selfnode));
  561. selfpara:=ctemprefnode.create(selftemp);
  562. end
  563. else
  564. selfpara:=selfnode;
  565. addstatement(statements,ccallnode.createintern('fpc_dispinvoke_variant',
  566. { parameters are passed always reverted, i.e. the last comes first }
  567. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  568. ccallparanode.create(caddrnode.create(cloadnode.create(calldescsym,current_module.localsymtable)),
  569. ccallparanode.create(ctypeconvnode.create_internal(selfpara,vardatadef),
  570. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  571. );
  572. if assigned(selftemp) then
  573. addstatement(statements,ctempdeletenode.create(selftemp));
  574. end
  575. else
  576. begin
  577. addstatement(statements,ccallnode.createintern('fpc_dispatch_by_id',
  578. { parameters are passed always reverted, i.e. the last comes first }
  579. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  580. ccallparanode.create(caddrnode.create(cloadnode.create(calldescsym,current_module.localsymtable)),
  581. ccallparanode.create(ctypeconvnode.create_internal(selfnode,voidpointertype),
  582. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  583. );
  584. end;
  585. addstatement(statements,ctempdeletenode.create(params));
  586. if useresult then
  587. begin
  588. { clean up }
  589. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  590. addstatement(statements,ctemprefnode.create(result_data));
  591. end;
  592. end;
  593. {****************************************************************************
  594. TOBJECTINFOITEM
  595. ****************************************************************************}
  596. constructor tobjectinfoitem.create(def : tobjectdef);
  597. begin
  598. inherited create;
  599. objinfo := def;
  600. end;
  601. {****************************************************************************
  602. TCALLPARANODE
  603. ****************************************************************************}
  604. procedure tcallparanode.handlemanagedbyrefpara(orgparadef: tdef);
  605. var
  606. temp: ttempcreatenode;
  607. npara: tcallparanode;
  608. paraaddrtype: tdef;
  609. begin
  610. { release memory for reference counted out parameters }
  611. if (parasym.varspez=vs_out) and
  612. is_managed_type(orgparadef) and
  613. (not is_open_array(resultdef) or
  614. is_managed_type(tarraydef(resultdef).elementdef)) and
  615. not(target_info.system in systems_garbage_collected_managed_types) then
  616. begin
  617. { after converting a parameter to an open array, its resultdef is
  618. set back to its original resultdef so we can get the value of the
  619. "high" parameter correctly, even though we already inserted a
  620. type conversion to "open array". Since here we work on this
  621. converted parameter, set it back to the type to which it was
  622. converted in order to avoid type mismatches at the LLVM level }
  623. if is_open_array(parasym.vardef) and
  624. is_dynamic_array(orgparadef) then
  625. begin
  626. left.resultdef:=resultdef;
  627. orgparadef:=resultdef;
  628. end;
  629. paraaddrtype:=cpointerdef.getreusable(orgparadef);
  630. { create temp with address of the parameter }
  631. temp:=ctempcreatenode.create(
  632. paraaddrtype,paraaddrtype.size,tt_persistent,true);
  633. { put this code in the init/done statement of the call node, because
  634. we should finalize all out parameters before other parameters
  635. are evaluated (in case e.g. a managed out parameter is also
  636. passed by value, we must not pass the pointer to the now possibly
  637. freed data as the value parameter, but the finalized/nil value }
  638. aktcallnode.add_init_statement(temp);
  639. aktcallnode.add_init_statement(
  640. cassignmentnode.create(
  641. ctemprefnode.create(temp),
  642. caddrnode.create(left)));
  643. if not is_open_array(resultdef) or
  644. not is_managed_type(tarraydef(resultdef).elementdef) then
  645. { finalize the entire parameter }
  646. aktcallnode.add_init_statement(
  647. cnodeutils.finalize_data_node(
  648. cderefnode.create(ctemprefnode.create(temp))))
  649. else
  650. begin
  651. { passing a (part of, in case of slice) dynamic array as an
  652. open array -> finalize the dynamic array contents, not the
  653. dynamic array itself }
  654. npara:=ccallparanode.create(
  655. { array length = high + 1 }
  656. caddnode.create(addn,third.getcopy,genintconstnode(1)),
  657. ccallparanode.create(caddrnode.create_internal
  658. (crttinode.create(tstoreddef(tarraydef(resultdef).elementdef),initrtti,rdt_normal)),
  659. ccallparanode.create(caddrnode.create_internal(
  660. cderefnode.create(ctemprefnode.create(temp))),nil)));
  661. aktcallnode.add_init_statement(
  662. ccallnode.createintern('fpc_finalize_array',npara));
  663. end;
  664. left:=cderefnode.create(ctemprefnode.create(temp));
  665. firstpass(left);
  666. aktcallnode.add_done_statement(ctempdeletenode.create(temp));
  667. end;
  668. end;
  669. procedure tcallparanode.copy_value_by_ref_para;
  670. var
  671. initstat,
  672. finistat: tstatementnode;
  673. finiblock: tblocknode;
  674. paratemp: ttempcreatenode;
  675. arraysize,
  676. arraybegin: tnode;
  677. lefttemp: ttempcreatenode;
  678. vardatatype,
  679. temparraydef: tdef;
  680. begin
  681. { this routine is for targets where by-reference value parameters need
  682. to be copied by the caller. It's basically the node-level equivalent
  683. of thlcgobj.g_copyvalueparas }
  684. if assigned(fparainit) then
  685. exit;
  686. { in case of an array constructor, we don't need a copy since the array
  687. constructor itself is already constructed on the fly (and hence if
  688. it's modified by the caller, that's no problem) }
  689. if not is_array_constructor(left.resultdef) then
  690. begin
  691. fparainit:=internalstatements(initstat);
  692. finiblock:=internalstatements(finistat);
  693. paratemp:=nil;
  694. { making a copy of an open array, an array of const or a dynamic
  695. array requires dynamic memory allocation since we don't know the
  696. size at compile time }
  697. if is_open_array(left.resultdef) or
  698. is_array_of_const(left.resultdef) or
  699. (is_dynamic_array(left.resultdef) and
  700. is_open_array(parasym.vardef)) then
  701. begin
  702. paratemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  703. if is_dynamic_array(left.resultdef) then
  704. begin
  705. { note that in insert_typeconv, this dynamic array was
  706. already converted into an open array (-> dereferenced)
  707. and then its resultdef was restored to the original
  708. dynamic array one -> get the address before treating it
  709. as a dynamic array here }
  710. { first restore the actual resultdef of left }
  711. temparraydef:=left.resultdef;
  712. left.resultdef:=resultdef;
  713. { get its address }
  714. lefttemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  715. addstatement(initstat,lefttemp);
  716. addstatement(finistat,ctempdeletenode.create(lefttemp));
  717. addstatement(initstat,
  718. cassignmentnode.create(
  719. ctemprefnode.create(lefttemp),
  720. caddrnode.create_internal(left)
  721. )
  722. );
  723. { now treat that address (correctly) as the original
  724. dynamic array to get its start and length }
  725. arraybegin:=cvecnode.create(
  726. ctypeconvnode.create_explicit(ctemprefnode.create(lefttemp),
  727. temparraydef),
  728. genintconstnode(0)
  729. );
  730. arraysize:=caddnode.create(muln,
  731. geninlinenode(in_length_x,false,
  732. ctypeconvnode.create_explicit(ctemprefnode.create(lefttemp),
  733. temparraydef)
  734. ),
  735. genintconstnode(tarraydef(temparraydef).elementdef.size)
  736. );
  737. end
  738. else
  739. begin
  740. { no problem here that left is used multiple times, as
  741. sizeof() will simply evaluate to the high parameter }
  742. arraybegin:=left.getcopy;
  743. arraysize:=geninlinenode(in_sizeof_x,false,left);
  744. end;
  745. addstatement(initstat,paratemp);
  746. { paratemp:=getmem(sizeof(para)) }
  747. addstatement(initstat,
  748. cassignmentnode.create(
  749. ctemprefnode.create(paratemp),
  750. ccallnode.createintern('fpc_getmem',
  751. ccallparanode.create(
  752. arraysize.getcopy,nil
  753. )
  754. )
  755. )
  756. );
  757. { move(para,temp,sizeof(arr)) (no "left.getcopy" below because
  758. we replace left afterwards) }
  759. addstatement(initstat,
  760. cifnode.create_internal(
  761. caddnode.create_internal(
  762. unequaln,
  763. arraysize.getcopy,
  764. genintconstnode(0)
  765. ),
  766. ccallnode.createintern('MOVE',
  767. ccallparanode.create(
  768. arraysize,
  769. ccallparanode.create(
  770. cderefnode.create(ctemprefnode.create(paratemp)),
  771. ccallparanode.create(
  772. arraybegin,nil
  773. )
  774. )
  775. )
  776. ),
  777. nil
  778. )
  779. );
  780. { no reference count increases, that's still done on the callee
  781. side because for compatibility with targets that perform this
  782. copy on the callee side, that should only be done for non-
  783. assember functions (and we can't know that 100% certain here,
  784. e.g. in case of external declarations) (*) }
  785. { free the memory again after the call: freemem(paratemp) }
  786. addstatement(finistat,
  787. ccallnode.createintern('fpc_freemem',
  788. ccallparanode.create(
  789. ctemprefnode.create(paratemp),nil
  790. )
  791. )
  792. );
  793. { replace the original parameter with a dereference of the
  794. temp typecasted to the same type as the original parameter
  795. (don't free left, it has been reused above) }
  796. left:=ctypeconvnode.create_internal(
  797. cderefnode.create(ctemprefnode.create(paratemp)),
  798. left.resultdef);
  799. end
  800. else if is_shortstring(parasym.vardef) then
  801. begin
  802. { the shortstring parameter may have a different size than the
  803. parameter type -> assign and truncate/extend }
  804. paratemp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,tt_persistent,false);
  805. addstatement(initstat,paratemp);
  806. { assign shortstring }
  807. addstatement(initstat,
  808. cassignmentnode.create(
  809. ctemprefnode.create(paratemp),left
  810. )
  811. );
  812. { replace parameter with temp (don't free left, it has been
  813. reused above) }
  814. left:=ctemprefnode.create(paratemp);
  815. end
  816. else if parasym.vardef.typ=variantdef then
  817. begin
  818. vardatatype:=search_system_type('TVARDATA').typedef;
  819. paratemp:=ctempcreatenode.create(vardatatype,vardatatype.size,tt_persistent,false);
  820. addstatement(initstat,paratemp);
  821. addstatement(initstat,
  822. ccallnode.createintern('fpc_variant_copy_overwrite',
  823. ccallparanode.create(
  824. ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),
  825. vardatatype
  826. ),
  827. ccallparanode.create(ctypeconvnode.create_explicit(left,
  828. vardatatype),
  829. nil
  830. )
  831. )
  832. )
  833. );
  834. { replace parameter with temp (don't free left, it has been
  835. reused above) }
  836. left:=ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),parasym.vardef);
  837. end
  838. else if is_managed_type(left.resultdef) then
  839. begin
  840. { don't increase/decrease the reference count here, will be done by
  841. the callee (see (*) above) -> typecast to array of byte
  842. for the assignment to the temp }
  843. temparraydef:=carraydef.getreusable(u8inttype,left.resultdef.size);
  844. paratemp:=ctempcreatenode.create(temparraydef,temparraydef.size,tt_persistent,false);
  845. addstatement(initstat,paratemp);
  846. addstatement(initstat,
  847. cassignmentnode.create(
  848. ctemprefnode.create(paratemp),
  849. ctypeconvnode.create_internal(left,temparraydef)
  850. )
  851. );
  852. left:=ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),left.resultdef);
  853. end
  854. else
  855. begin
  856. paratemp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  857. addstatement(initstat,paratemp);
  858. addstatement(initstat,
  859. cassignmentnode.create(ctemprefnode.create(paratemp),left)
  860. );
  861. { replace parameter with temp (don't free left, it has been
  862. reused above) }
  863. left:=ctemprefnode.create(paratemp);
  864. end;
  865. { add the finish statements to the call cleanup block }
  866. addstatement(finistat,ctempdeletenode.create(paratemp));
  867. aktcallnode.add_done_statement(finiblock);
  868. firstpass(fparainit);
  869. firstpass(left);
  870. end;
  871. end;
  872. constructor tcallparanode.create(expr,next : tnode);
  873. begin
  874. inherited create(callparan,expr,next,nil);
  875. if not assigned(expr) then
  876. internalerror(200305091);
  877. expr.fileinfo:=fileinfo;
  878. callparaflags:=[];
  879. originalindex:=-1;
  880. if expr.nodetype = typeconvn then
  881. ttypeconvnode(expr).warn_pointer_to_signed:=false;
  882. end;
  883. destructor tcallparanode.destroy;
  884. begin
  885. fparainit.free;
  886. fparacopyback.free;
  887. inherited destroy;
  888. end;
  889. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  890. begin
  891. inherited ppuload(t,ppufile);
  892. originalindex:=-1;
  893. ppufile.getset(tppuset1(callparaflags));
  894. fparainit:=ppuloadnode(ppufile);
  895. fparacopyback:=ppuloadnode(ppufile);
  896. end;
  897. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  898. begin
  899. inherited ppuwrite(ppufile);
  900. ppufile.putset(tppuset1(callparaflags));
  901. ppuwritenode(ppufile,fparainit);
  902. ppuwritenode(ppufile,fparacopyback);
  903. end;
  904. procedure tcallparanode.buildderefimpl;
  905. begin
  906. inherited buildderefimpl;
  907. if assigned(fparainit) then
  908. fparainit.buildderefimpl;
  909. if assigned(fparacopyback) then
  910. fparacopyback.buildderefimpl;
  911. end;
  912. procedure tcallparanode.derefimpl;
  913. begin
  914. inherited derefimpl;
  915. if assigned(fparainit) then
  916. fparainit.derefimpl;
  917. if assigned(fparacopyback) then
  918. fparacopyback.derefimpl;
  919. end;
  920. function tcallparanode.dogetcopy : tnode;
  921. var
  922. n : tcallparanode;
  923. initcopy: tnode;
  924. begin
  925. initcopy:=nil;
  926. { must be done before calling inherited getcopy, because can create
  927. tempcreatenodes for values used in left }
  928. if assigned(fparainit) then
  929. initcopy:=fparainit.getcopy;
  930. n:=tcallparanode(inherited dogetcopy);
  931. n.callparaflags:=callparaflags;
  932. n.originalindex:=originalindex;
  933. n.parasym:=parasym;
  934. n.fparainit:=initcopy;
  935. if assigned(fparacopyback) then
  936. n.fparacopyback:=fparacopyback.getcopy;
  937. result:=n;
  938. end;
  939. procedure tcallparanode.insertintolist(l : tnodelist);
  940. begin
  941. end;
  942. function tcallparanode.pass_typecheck : tnode;
  943. begin
  944. { need to use get_paratype }
  945. internalerror(200709251);
  946. result:=nil;
  947. end;
  948. function tcallparanode.pass_1 : tnode;
  949. begin
  950. { need to use firstcallparan }
  951. internalerror(200709252);
  952. result:=nil;
  953. end;
  954. procedure tcallparanode.get_paratype;
  955. begin
  956. if assigned(right) then
  957. tcallparanode(right).get_paratype;
  958. if assigned(fparainit) then
  959. typecheckpass(fparainit);
  960. typecheckpass(left);
  961. if assigned(third) then
  962. typecheckpass(third);
  963. if assigned(fparacopyback) then
  964. typecheckpass(fparacopyback);
  965. if codegenerror then
  966. resultdef:=generrordef
  967. else
  968. resultdef:=left.resultdef;
  969. end;
  970. procedure tcallparanode.firstcallparan;
  971. begin
  972. if assigned(right) then
  973. tcallparanode(right).firstcallparan;
  974. if not assigned(left.resultdef) then
  975. get_paratype;
  976. if assigned(parasym) and
  977. (parasym.varspez in [vs_var,vs_out,vs_constref]) and
  978. { for record constructors }
  979. (left.nodetype<>nothingn) then
  980. handlemanagedbyrefpara(left.resultdef);
  981. { for targets that have to copy "value parameters by reference" on the
  982. caller side
  983. aktcallnode may not be assigned in case firstcallparan is called for
  984. fake parameters to inline nodes (in that case, we don't have a real
  985. call and hence no "caller side" either)
  986. }
  987. if assigned(aktcallnode) and
  988. (target_info.system in systems_caller_copy_addr_value_para) and
  989. ((assigned(parasym) and
  990. (parasym.varspez=vs_value)) or
  991. (cpf_varargs_para in callparaflags)) and
  992. (left.nodetype<>nothingn) and
  993. not(vo_has_local_copy in parasym.varoptions) and
  994. ((not is_open_array(parasym.vardef) and
  995. not is_array_of_const(parasym.vardef)) or
  996. not(aktcallnode.procdefinition.proccalloption in cdecl_pocalls)) and
  997. paramanager.push_addr_param(vs_value,parasym.vardef,
  998. aktcallnode.procdefinition.proccalloption) then
  999. copy_value_by_ref_para;
  1000. if assigned(fparainit) then
  1001. firstpass(fparainit);
  1002. firstpass(left);
  1003. if assigned(fparacopyback) then
  1004. firstpass(fparacopyback);
  1005. if assigned(third) then
  1006. firstpass(third);
  1007. expectloc:=left.expectloc;
  1008. end;
  1009. procedure tcallparanode.insert_typeconv;
  1010. var
  1011. olddef : tdef;
  1012. hp : tnode;
  1013. block : tblocknode;
  1014. statements : tstatementnode;
  1015. temp : ttempcreatenode;
  1016. owningprocdef: tprocdef;
  1017. begin
  1018. { Be sure to have the resultdef }
  1019. if not assigned(left.resultdef) then
  1020. typecheckpass(left);
  1021. if (left.nodetype<>nothingn) then
  1022. begin
  1023. { convert loads of the function result variable into procvars
  1024. representing the current function in case the formal parameter is
  1025. a procvar (CodeWarrior Pascal contains the same kind of
  1026. automatic disambiguation; you can use the function name in both
  1027. meanings, so we cannot statically pick either the function result
  1028. or the function definition in pexpr) }
  1029. if (m_mac in current_settings.modeswitches) and
  1030. (parasym.vardef.typ=procvardef) and
  1031. is_ambiguous_funcret_load(left,owningprocdef) then
  1032. begin
  1033. hp:=cloadnode.create_procvar(owningprocdef.procsym,owningprocdef,owningprocdef.procsym.owner);
  1034. typecheckpass(hp);
  1035. left.free;
  1036. left:=hp;
  1037. end;
  1038. { Convert tp procvars, this is needs to be done
  1039. here to make the change permanent. in the overload
  1040. choosing the changes are only made temporarily
  1041. Don't do this for parentfp parameters, as for calls to nested
  1042. procvars they are a copy of right, which is the procvar itself
  1043. and hence turning that into a call would result into endless
  1044. recursion. For regular nested calls, the parentfp node can
  1045. never be a procvar (it's a loadparentfpnode). }
  1046. if not(vo_is_parentfp in parasym.varoptions) and
  1047. (left.resultdef.typ=procvardef) and
  1048. not(parasym.vardef.typ in [procvardef,formaldef]) then
  1049. begin
  1050. if maybe_call_procvar(left,true) then
  1051. resultdef:=left.resultdef
  1052. end;
  1053. { Remove implicitly inserted typecast to pointer for
  1054. @procvar in macpas }
  1055. if (m_mac_procvar in current_settings.modeswitches) and
  1056. (parasym.vardef.typ=procvardef) and
  1057. (left.nodetype=typeconvn) and
  1058. is_voidpointer(left.resultdef) and
  1059. (ttypeconvnode(left).left.nodetype=typeconvn) and
  1060. (ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
  1061. begin
  1062. hp:=left;
  1063. left:=ttypeconvnode(left).left;
  1064. ttypeconvnode(hp).left:=nil;
  1065. hp.free;
  1066. end;
  1067. maybe_global_proc_to_nested(left,parasym.vardef);
  1068. { Handle varargs and hidden paras directly, no typeconvs or }
  1069. { pass_typechecking needed }
  1070. if (cpf_varargs_para in callparaflags) then
  1071. begin
  1072. { this should only happen vor C varargs }
  1073. { the necessary conversions have already been performed in }
  1074. { tarrayconstructornode.insert_typeconvs }
  1075. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1076. insert_varargstypeconv(left,true);
  1077. resultdef:=left.resultdef;
  1078. { also update parasym type to get the correct parameter location
  1079. for the new types }
  1080. parasym.vardef:=left.resultdef;
  1081. end
  1082. else
  1083. if (vo_is_hidden_para in parasym.varoptions) then
  1084. begin
  1085. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1086. resultdef:=left.resultdef;
  1087. end
  1088. else
  1089. begin
  1090. { Do we need arrayconstructor -> set conversion, then insert
  1091. it here before the arrayconstructor node breaks the tree
  1092. with its conversions of enum->ord }
  1093. if (left.nodetype=arrayconstructorn) and
  1094. (parasym.vardef.typ=setdef) then
  1095. inserttypeconv(left,parasym.vardef);
  1096. { if an array constructor can be a set and it is passed to
  1097. a formaldef, a set must be passed, see also issue #37796 }
  1098. if (left.nodetype=arrayconstructorn) and
  1099. (parasym.vardef.typ=formaldef) and
  1100. (arrayconstructor_can_be_set(left)) then
  1101. left:=arrayconstructor_to_set(left,false);
  1102. { set some settings needed for arrayconstructor }
  1103. if is_array_constructor(left.resultdef) then
  1104. begin
  1105. if left.nodetype<>arrayconstructorn then
  1106. internalerror(200504041);
  1107. if is_array_of_const(parasym.vardef) then
  1108. begin
  1109. { force variant array }
  1110. include(tarrayconstructornode(left).arrayconstructornodeflags,acnf_forcevaria);
  1111. end
  1112. else
  1113. begin
  1114. include(tarrayconstructornode(left).arrayconstructornodeflags,acnf_novariaallowed);
  1115. { now that the resultting type is know we can insert the required
  1116. typeconvs for the array constructor }
  1117. if parasym.vardef.typ=arraydef then
  1118. tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
  1119. end;
  1120. end;
  1121. { check if local proc/func is assigned to procvar }
  1122. if left.resultdef.typ=procvardef then
  1123. test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
  1124. { test conversions }
  1125. if not(is_shortstring(left.resultdef) and
  1126. is_shortstring(parasym.vardef)) and
  1127. (parasym.vardef.typ<>formaldef) and
  1128. not(parasym.univpara) then
  1129. begin
  1130. { Process open parameters }
  1131. if paramanager.keep_para_array_range(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
  1132. begin
  1133. { insert type conv but hold the ranges of the array }
  1134. olddef:=left.resultdef;
  1135. inserttypeconv(left,parasym.vardef);
  1136. left.resultdef:=olddef;
  1137. end
  1138. else
  1139. begin
  1140. check_ranges(left.fileinfo,left,parasym.vardef);
  1141. inserttypeconv(left,parasym.vardef);
  1142. end;
  1143. if codegenerror then
  1144. exit;
  1145. end;
  1146. { truncate shortstring value parameters at the caller side if }
  1147. { they are passed by value (if passed by reference, then the }
  1148. { callee will truncate when copying in the string) }
  1149. { This happens e.g. on x86_64 for small strings }
  1150. if is_shortstring(left.resultdef) and
  1151. is_shortstring(parasym.vardef) and
  1152. (parasym.varspez=vs_value) and
  1153. not paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  1154. aktcallnode.procdefinition.proccalloption) and
  1155. ((is_open_string(left.resultdef) and
  1156. (tstringdef(parasym.vardef).len < 255)) or
  1157. (not is_open_string(left.resultdef) and
  1158. { when a stringconstn is typeconverted, then only its }
  1159. { def is modified, not the contents (needed because in }
  1160. { Delphi/TP, if you pass a longer string to a const }
  1161. { parameter, then the callee has to see this longer }
  1162. { string) }
  1163. (((left.nodetype<>stringconstn) and
  1164. (tstringdef(parasym.vardef).len<tstringdef(left.resultdef).len)) or
  1165. ((left.nodetype=stringconstn) and
  1166. (tstringdef(parasym.vardef).len<tstringconstnode(left).len))))) then
  1167. begin
  1168. block:=internalstatements(statements);
  1169. { temp for the new string }
  1170. temp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,
  1171. tt_persistent,true);
  1172. addstatement(statements,temp);
  1173. { assign parameter to temp }
  1174. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  1175. left:=nil;
  1176. { release temp after next use }
  1177. addstatement(statements,ctempdeletenode.create_normal_temp(temp));
  1178. addstatement(statements,ctemprefnode.create(temp));
  1179. typecheckpass(tnode(block));
  1180. left:=block;
  1181. end;
  1182. { check var strings }
  1183. if (cs_strict_var_strings in current_settings.localswitches) and
  1184. is_shortstring(left.resultdef) and
  1185. is_shortstring(parasym.vardef) and
  1186. (parasym.varspez in [vs_out,vs_var,vs_constref]) and
  1187. not(is_open_string(parasym.vardef)) and
  1188. not(equal_defs(left.resultdef,parasym.vardef)) then
  1189. begin
  1190. CGMessagePos(left.fileinfo,type_e_strict_var_string_violation);
  1191. end;
  1192. { passing a value to an "univ" parameter implies an explicit
  1193. typecast to the parameter type. Must be done before the
  1194. valid_for_var() check, since the typecast can result in
  1195. an invalid lvalue in case of var/out parameters. }
  1196. if (parasym.univpara) then
  1197. begin
  1198. { load procvar if a procedure is passed }
  1199. if ((m_tp_procvar in current_settings.modeswitches) or
  1200. (m_mac_procvar in current_settings.modeswitches)) and
  1201. (left.nodetype=calln) and
  1202. (is_void(left.resultdef)) then
  1203. begin
  1204. load_procvar_from_calln(left);
  1205. { load_procvar_from_calln() creates a loadn for a
  1206. a procedure, which means that the type conversion
  1207. below will type convert the first instruction
  1208. bytes of the procedure -> convert to a procvar }
  1209. left:=ctypeconvnode.create_proc_to_procvar(left);
  1210. typecheckpass(left);
  1211. end;
  1212. inserttypeconv_explicit(left,parasym.vardef);
  1213. end;
  1214. { Handle formal parameters separate }
  1215. if (parasym.vardef.typ=formaldef) then
  1216. begin
  1217. { load procvar if a procedure is passed }
  1218. if ((m_tp_procvar in current_settings.modeswitches) or
  1219. (m_mac_procvar in current_settings.modeswitches)) and
  1220. (left.nodetype=calln) and
  1221. (is_void(left.resultdef)) then
  1222. load_procvar_from_calln(left);
  1223. case parasym.varspez of
  1224. vs_var,
  1225. vs_out :
  1226. begin
  1227. if not valid_for_formal_var(left,true) then
  1228. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  1229. end;
  1230. vs_constref:
  1231. begin
  1232. if not valid_for_formal_constref(left,true) then
  1233. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  1234. end;
  1235. vs_const :
  1236. begin
  1237. if not valid_for_formal_const(left,true) then
  1238. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list)
  1239. else if (target_info.system in systems_managed_vm) and
  1240. (left.resultdef.typ in [orddef,floatdef]) then
  1241. begin
  1242. left:=cinlinenode.create(in_box_x,false,ccallparanode.create(left,nil));
  1243. typecheckpass(left);
  1244. end;
  1245. end;
  1246. else
  1247. ;
  1248. end;
  1249. end
  1250. else
  1251. begin
  1252. { check if the argument is allowed }
  1253. if (parasym.varspez in [vs_out,vs_var]) then
  1254. valid_for_var(left,true);
  1255. end;
  1256. if parasym.varspez in [vs_var,vs_out,vs_constref] then
  1257. set_unique(left);
  1258. if (parasym.varspez=vs_const) and (parasym.vardef.typ=formaldef) then
  1259. begin
  1260. { compilerprocs never capture the address of their
  1261. parameters }
  1262. if (po_compilerproc in aktcallnode.procdefinition.procoptions) or
  1263. { if we handled already the proc. body and it is not inlined,
  1264. we can propagate the information if the address of a parameter is taken or not }
  1265. ((aktcallnode.procdefinition.typ=procdef) and
  1266. not(po_inline in tprocdef(aktcallnode.procdefinition).procoptions) and
  1267. (tprocdef(aktcallnode.procdefinition).is_implemented) and
  1268. not(parasym.addr_taken)) then
  1269. make_not_regable(left,[ra_addr_regable])
  1270. else
  1271. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  1272. end
  1273. else
  1274. case parasym.varspez of
  1275. vs_out :
  1276. begin
  1277. { first set written separately to avoid false }
  1278. { uninitialized warnings (tbs/tb0542) }
  1279. set_varstate(left,vs_written,[]);
  1280. set_varstate(left,vs_readwritten,[]);
  1281. { compilerprocs never capture the address of their
  1282. parameters }
  1283. if (po_compilerproc in aktcallnode.procdefinition.procoptions) or
  1284. { if we handled already the proc. body and it is not inlined,
  1285. we can propagate the information if the address of a parameter is taken or not }
  1286. ((aktcallnode.procdefinition.typ=procdef) and
  1287. not(po_inline in tprocdef(aktcallnode.procdefinition).procoptions) and
  1288. (tprocdef(aktcallnode.procdefinition).is_implemented) and
  1289. not(parasym.addr_taken)) then
  1290. make_not_regable(left,[ra_addr_regable])
  1291. else
  1292. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  1293. end;
  1294. vs_var:
  1295. begin
  1296. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  1297. { compilerprocs never capture the address of their
  1298. parameters }
  1299. if (po_compilerproc in aktcallnode.procdefinition.procoptions) or
  1300. { if we handled already the proc. body and it is not inlined,
  1301. we can propagate the information if the address of a parameter is taken or not }
  1302. ((aktcallnode.procdefinition.typ=procdef) and
  1303. not(po_inline in tprocdef(aktcallnode.procdefinition).procoptions) and
  1304. (tprocdef(aktcallnode.procdefinition).is_implemented) and
  1305. not(parasym.addr_taken)) then
  1306. make_not_regable(left,[ra_addr_regable])
  1307. else
  1308. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  1309. end;
  1310. vs_constref:
  1311. begin
  1312. { constref does not mean that the variable is actually written, this might only
  1313. happen if it's address is taken, this is handled below }
  1314. set_varstate(left,vs_read,[vsf_must_be_valid,vsf_use_hints]);
  1315. { compilerprocs never capture the address of their
  1316. parameters }
  1317. if (po_compilerproc in aktcallnode.procdefinition.procoptions) or
  1318. { if we handled already the proc. body and it is not inlined,
  1319. we can propagate the information if the address of a parameter is taken or not }
  1320. ((aktcallnode.procdefinition.typ=procdef) and
  1321. not(po_inline in tprocdef(aktcallnode.procdefinition).procoptions) and
  1322. (tprocdef(aktcallnode.procdefinition).is_implemented) and
  1323. not(parasym.addr_taken)) then
  1324. make_not_regable(left,[ra_addr_regable])
  1325. else
  1326. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  1327. end;
  1328. else
  1329. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1330. end;
  1331. { must only be done after typeconv PM }
  1332. resultdef:=parasym.vardef;
  1333. end;
  1334. end;
  1335. { process next node }
  1336. if assigned(right) then
  1337. tcallparanode(right).insert_typeconv;
  1338. end;
  1339. function tcallparanode.can_be_inlined: boolean;
  1340. var
  1341. n: tnode;
  1342. begin
  1343. n:=left;
  1344. result:=false;
  1345. while assigned(n) and
  1346. (n.nodetype=typeconvn) do
  1347. begin
  1348. { look for type conversion nodes which convert a }
  1349. { refcounted type into a non-refcounted type }
  1350. if not is_managed_type(n.resultdef) and
  1351. is_managed_type(ttypeconvnode(n).left.resultdef) then
  1352. exit;
  1353. n:=ttypeconvnode(n).left;
  1354. end;
  1355. { also check for dereferencing constant pointers, like }
  1356. { tsomerecord(nil^) passed to a const r: tsomerecord }
  1357. { parameter }
  1358. if (n.nodetype=derefn) then
  1359. begin
  1360. repeat
  1361. n:=tunarynode(n).left;
  1362. until (n.nodetype<>typeconvn);
  1363. if (n.nodetype in [niln,pointerconstn]) then
  1364. exit
  1365. end;
  1366. result:=true;
  1367. end;
  1368. function check_contains_stack_tainting_call(var n: tnode; arg: pointer): foreachnoderesult;
  1369. begin
  1370. if (n.nodetype=calln) and
  1371. tcallnode(n).procdefinition.stack_tainting_parameter(callerside) then
  1372. result:=fen_norecurse_true
  1373. else
  1374. result:=fen_false;
  1375. end;
  1376. function tcallparanode.contains_stack_tainting_call: boolean;
  1377. begin
  1378. result:=foreachnodestatic(pm_postprocess,left,@check_contains_stack_tainting_call,nil);
  1379. end;
  1380. procedure tcallparanode.init_contains_stack_tainting_call_cache;
  1381. begin
  1382. fcontains_stack_tainting_call_cached:=contains_stack_tainting_call;
  1383. end;
  1384. function tcallparanode.docompare(p: tnode): boolean;
  1385. begin
  1386. docompare :=
  1387. inherited docompare(p) and
  1388. fparainit.isequal(tcallparanode(p).fparainit) and
  1389. fparacopyback.isequal(tcallparanode(p).fparacopyback) and
  1390. (callparaflags = tcallparanode(p).callparaflags)
  1391. ;
  1392. end;
  1393. procedure tcallparanode.printnodetree(var t:text);
  1394. var
  1395. hp: tbinarynode;
  1396. begin
  1397. hp:=self;
  1398. while assigned(hp) do
  1399. begin
  1400. write(t,printnodeindention,'(');
  1401. printnodeindent;
  1402. hp.printnodeinfo(t);
  1403. writeln(t);
  1404. if assigned(tcallparanode(hp).fparainit) then
  1405. begin
  1406. writeln(t,printnodeindention,'(parainit =');
  1407. printnodeindent;
  1408. printnode(t,tcallparanode(hp).fparainit);
  1409. printnodeunindent;
  1410. writeln(t,printnodeindention,')');
  1411. end;
  1412. if assigned(tcallparanode(hp).fparacopyback) then
  1413. begin
  1414. writeln(t,printnodeindention,'(fparacopyback =');
  1415. printnodeindent;
  1416. printnode(t,tcallparanode(hp).fparacopyback);
  1417. printnodeunindent;
  1418. writeln(t,printnodeindention,')');
  1419. end;
  1420. printnode(t,hp.left);
  1421. writeln(t);
  1422. printnodeunindent;
  1423. writeln(t,printnodeindention,')');
  1424. hp:=tbinarynode(hp.right);
  1425. end;
  1426. end;
  1427. {****************************************************************************
  1428. TCALLNODE
  1429. ****************************************************************************}
  1430. constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags;sc:tspecializationcontext);
  1431. var
  1432. srsym: tsym;
  1433. srsymtable: tsymtable;
  1434. begin
  1435. inherited create(calln,l,nil);
  1436. spezcontext:=sc;
  1437. symtableprocentry:=v;
  1438. symtableproc:=st;
  1439. callnodeflags:=callflags+[cnf_return_value_used];
  1440. methodpointer:=mp;
  1441. callinitblock:=nil;
  1442. callcleanupblock:=nil;
  1443. procdefinition:=nil;
  1444. funcretnode:=nil;
  1445. paralength:=-1;
  1446. varargsparas:=nil;
  1447. intrinsiccode:=Default(TInlineNumber);
  1448. if assigned(current_structdef) and
  1449. assigned(mp) and
  1450. assigned(current_procinfo) then
  1451. begin
  1452. { only needed when calling a destructor from an exception block in a
  1453. contructor of a TP-style object }
  1454. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1455. (cnf_create_failed in callflags) then
  1456. if is_object(current_structdef) then
  1457. call_vmt_node:=load_vmt_pointer_node
  1458. else if is_class(current_structdef) then
  1459. begin
  1460. if not searchsym(copy(internaltypeprefixName[itp_vmt_afterconstruction_local],2,255),srsym,srsymtable) then
  1461. internalerror(2016090801);
  1462. call_vmt_node:=cloadnode.create(srsym,srsymtable);
  1463. end;
  1464. end;
  1465. end;
  1466. constructor tcallnode.create_procvar(l,r:tnode);
  1467. begin
  1468. create(l,nil,nil,nil,[],nil);
  1469. right:=r;
  1470. end;
  1471. constructor tcallnode.createintern(const name: string; params: tnode);
  1472. var
  1473. srsym: tsym;
  1474. begin
  1475. srsym := tsym(systemunit.Find(name));
  1476. { in case we are looking for a non-external compilerproc of which we
  1477. only have parsed the declaration until now (the symbol name will
  1478. still be uppercased, because it needs to be matched when we
  1479. encounter the implementation) }
  1480. if not assigned(srsym) and
  1481. (cs_compilesystem in current_settings.moduleswitches) then
  1482. srsym := tsym(systemunit.Find(upper(name)));
  1483. if not assigned(srsym) or
  1484. (srsym.typ<>procsym) then
  1485. Message1(cg_f_unknown_compilerproc,name);
  1486. create(params,tprocsym(srsym),srsym.owner,nil,[],nil);
  1487. end;
  1488. constructor tcallnode.createfromintrinsic(const intrinsic: TInlineNumber; const name: string; params: tnode);
  1489. begin
  1490. createintern(name, params);
  1491. intrinsiccode := intrinsic;
  1492. end;
  1493. constructor tcallnode.createinternfromunit(const fromunit, procname: string; params: tnode);
  1494. var
  1495. srsym: tsym;
  1496. srsymtable: tsymtable;
  1497. begin
  1498. srsym:=nil;
  1499. if not searchsym_in_named_module(fromunit,procname,srsym,srsymtable) or
  1500. (srsym.typ<>procsym) then
  1501. Message1(cg_f_unknown_compilerproc,fromunit+'.'+procname);
  1502. create(params,tprocsym(srsym),srsymtable,nil,[],nil);
  1503. end;
  1504. constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
  1505. var
  1506. pd : tprocdef;
  1507. begin
  1508. createintern(name,params);
  1509. typedef:=res;
  1510. include(callnodeflags,cnf_typedefset);
  1511. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1512. { both the normal and specified resultdef either have to be returned via a }
  1513. { parameter or not, but no mixing (JM) }
  1514. if paramanager.ret_in_param(typedef,pd) xor
  1515. paramanager.ret_in_param(pd.returndef,pd) then
  1516. internalerror(2001082911);
  1517. end;
  1518. constructor tcallnode.createinternresfromunit(const fromunit, procname: string; params: tnode; res:tdef);
  1519. var
  1520. pd : tprocdef;
  1521. begin
  1522. createinternfromunit(fromunit,procname,params);
  1523. typedef:=res;
  1524. include(callnodeflags,cnf_typedefset);
  1525. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1526. { both the normal and specified resultdef either have to be returned via a }
  1527. { parameter or not, but no mixing (JM) }
  1528. if paramanager.ret_in_param(typedef,pd) xor
  1529. paramanager.ret_in_param(pd.returndef,pd) then
  1530. internalerror(200108291);
  1531. end;
  1532. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  1533. begin
  1534. createintern(name,params);
  1535. funcretnode:=returnnode;
  1536. end;
  1537. constructor tcallnode.createinternmethod(mp: tnode; const name: string; params: tnode);
  1538. var
  1539. ps: tsym;
  1540. recdef: tabstractrecorddef;
  1541. begin
  1542. typecheckpass(mp);
  1543. if mp.resultdef.typ=classrefdef then
  1544. recdef:=tabstractrecorddef(tclassrefdef(mp.resultdef).pointeddef)
  1545. else
  1546. recdef:=tabstractrecorddef(mp.resultdef);
  1547. ps:=search_struct_member(recdef,name);
  1548. if not assigned(ps) or
  1549. (ps.typ<>procsym) then
  1550. internalerror(2011062806);
  1551. create(params,tprocsym(ps),ps.owner,mp,[],nil);
  1552. end;
  1553. constructor tcallnode.createinternmethodres(mp: tnode; const name: string; params: tnode; res: tdef);
  1554. begin
  1555. createinternmethod(mp,name,params);
  1556. typedef:=res;
  1557. include(callnodeflags,cnf_typedefset)
  1558. end;
  1559. destructor tcallnode.destroy;
  1560. begin
  1561. methodpointer.free;
  1562. callinitblock.free;
  1563. callcleanupblock.free;
  1564. funcretnode.free;
  1565. if assigned(varargsparas) then
  1566. varargsparas.free;
  1567. call_self_node.free;
  1568. call_vmt_node.free;
  1569. vmt_entry.free;
  1570. spezcontext.free;
  1571. inherited destroy;
  1572. end;
  1573. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1574. begin
  1575. callinitblock:=tblocknode(ppuloadnode(ppufile));
  1576. methodpointer:=ppuloadnode(ppufile);
  1577. call_self_node:=ppuloadnode(ppufile);
  1578. call_vmt_node:=ppuloadnode(ppufile);
  1579. callcleanupblock:=tblocknode(ppuloadnode(ppufile));
  1580. funcretnode:=ppuloadnode(ppufile);
  1581. inherited ppuload(t,ppufile);
  1582. ppufile.getderef(symtableprocentryderef);
  1583. { TODO: FIXME: No withsymtable support}
  1584. symtableproc:=nil;
  1585. ppufile.getderef(procdefinitionderef);
  1586. ppufile.getset(tppuset4(callnodeflags));
  1587. intrinsiccode:=TInlineNumber(ppufile.getword);
  1588. end;
  1589. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  1590. begin
  1591. ppuwritenode(ppufile,callinitblock);
  1592. ppuwritenode(ppufile,methodpointer);
  1593. ppuwritenode(ppufile,call_self_node);
  1594. ppuwritenode(ppufile,call_vmt_node);
  1595. ppuwritenode(ppufile,callcleanupblock);
  1596. ppuwritenode(ppufile,funcretnode);
  1597. inherited ppuwrite(ppufile);
  1598. ppufile.putderef(symtableprocentryderef);
  1599. ppufile.putderef(procdefinitionderef);
  1600. ppufile.putset(tppuset4(callnodeflags));
  1601. ppufile.putword(word(intrinsiccode));
  1602. end;
  1603. procedure tcallnode.buildderefimpl;
  1604. begin
  1605. inherited buildderefimpl;
  1606. symtableprocentryderef.build(symtableprocentry);
  1607. procdefinitionderef.build(procdefinition);
  1608. if assigned(methodpointer) then
  1609. methodpointer.buildderefimpl;
  1610. if assigned(call_self_node) then
  1611. call_self_node.buildderefimpl;
  1612. if assigned(call_vmt_node) then
  1613. call_vmt_node.buildderefimpl;
  1614. if assigned(callinitblock) then
  1615. callinitblock.buildderefimpl;
  1616. if assigned(callcleanupblock) then
  1617. callcleanupblock.buildderefimpl;
  1618. if assigned(funcretnode) then
  1619. funcretnode.buildderefimpl;
  1620. end;
  1621. procedure tcallnode.derefimpl;
  1622. var
  1623. pt : tcallparanode;
  1624. i : integer;
  1625. begin
  1626. inherited derefimpl;
  1627. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  1628. if assigned(symtableprocentry) then
  1629. symtableproc:=symtableprocentry.owner;
  1630. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  1631. if assigned(methodpointer) then
  1632. methodpointer.derefimpl;
  1633. if assigned(call_self_node) then
  1634. call_self_node.derefimpl;
  1635. if assigned(call_vmt_node) then
  1636. call_vmt_node.derefimpl;
  1637. if assigned(callinitblock) then
  1638. callinitblock.derefimpl;
  1639. if assigned(callcleanupblock) then
  1640. callcleanupblock.derefimpl;
  1641. if assigned(funcretnode) then
  1642. funcretnode.derefimpl;
  1643. { generic method has no procdefinition }
  1644. if assigned(procdefinition) then
  1645. begin
  1646. { Connect parasyms }
  1647. pt:=tcallparanode(left);
  1648. while assigned(pt) and
  1649. (cpf_varargs_para in pt.callparaflags) do
  1650. pt:=tcallparanode(pt.right);
  1651. for i:=procdefinition.paras.count-1 downto 0 do
  1652. begin
  1653. if not assigned(pt) then
  1654. internalerror(200311077);
  1655. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  1656. pt:=tcallparanode(pt.right);
  1657. end;
  1658. if assigned(pt) then
  1659. internalerror(200311078);
  1660. end;
  1661. end;
  1662. function tcallnode.dogetcopy : tnode;
  1663. var
  1664. n : tcallnode;
  1665. i : integer;
  1666. hp,hpn : tparavarsym;
  1667. oldleft, oldright : tnode;
  1668. para: tcallparanode;
  1669. begin
  1670. { Need to use a hack here to prevent the parameters from being copied.
  1671. The parameters must be copied between callinitblock/callcleanupblock because
  1672. they can reference methodpointer }
  1673. { same goes for right (= self/context for procvars) }
  1674. oldleft:=left;
  1675. left:=nil;
  1676. oldright:=right;
  1677. right:=nil;
  1678. n:=tcallnode(inherited dogetcopy);
  1679. left:=oldleft;
  1680. right:=oldright;
  1681. n.symtableprocentry:=symtableprocentry;
  1682. n.symtableproc:=symtableproc;
  1683. n.procdefinition:=procdefinition;
  1684. n.typedef := typedef;
  1685. n.callnodeflags := callnodeflags;
  1686. n.pushedparasize := pushedparasize;
  1687. n.intrinsiccode := intrinsiccode;
  1688. if assigned(callinitblock) then
  1689. n.callinitblock:=tblocknode(callinitblock.dogetcopy)
  1690. else
  1691. n.callinitblock:=nil;
  1692. { callinitblock is copied, now references to the temp will also be copied
  1693. correctly. We can now copy the parameters, funcret and methodpointer }
  1694. if assigned(left) then
  1695. n.left:=left.dogetcopy
  1696. else
  1697. n.left:=nil;
  1698. if assigned(right) then
  1699. n.right:=right.dogetcopy
  1700. else
  1701. n.right:=nil;
  1702. if assigned(methodpointer) then
  1703. n.methodpointer:=methodpointer.dogetcopy
  1704. else
  1705. n.methodpointer:=nil;
  1706. if assigned(call_self_node) then
  1707. n.call_self_node:=call_self_node.dogetcopy
  1708. else
  1709. n.call_self_node:=nil;
  1710. if assigned(call_vmt_node) then
  1711. n.call_vmt_node:=call_vmt_node.dogetcopy
  1712. else
  1713. n.call_vmt_node:=nil;
  1714. if assigned(vmt_entry) then
  1715. n.vmt_entry:=vmt_entry.dogetcopy
  1716. else
  1717. n.vmt_entry:=nil;
  1718. { must be copied before the funcretnode, because the callcleanup block
  1719. may contain a ttempdeletenode that sets the tempinfo of the
  1720. corresponding temp to ti_nextref_set_hookoncopy_nil, and this nextref
  1721. itself may be the funcretnode }
  1722. if assigned(callcleanupblock) then
  1723. n.callcleanupblock:=tblocknode(callcleanupblock.dogetcopy)
  1724. else
  1725. n.callcleanupblock:=nil;
  1726. if assigned(funcretnode) then
  1727. n.funcretnode:=funcretnode.dogetcopy
  1728. else
  1729. n.funcretnode:=nil;
  1730. if assigned(varargsparas) then
  1731. begin
  1732. n.varargsparas:=tvarargsparalist.create(true);
  1733. n.varargsparas.capacity:=varargsparas.count;
  1734. for i:=0 to varargsparas.count-1 do
  1735. begin
  1736. hp:=tparavarsym(varargsparas[i]);
  1737. hpn:=cparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1738. n.varargsparas.add(hpn);
  1739. para:=tcallparanode(n.left);
  1740. while assigned(para) do
  1741. begin
  1742. if (para.parasym=hp) then
  1743. para.parasym:=hpn;
  1744. para:=tcallparanode(para.right);
  1745. end;
  1746. end;
  1747. end
  1748. else
  1749. n.varargsparas:=nil;
  1750. n.foverrideprocnamedef:=foverrideprocnamedef;
  1751. result:=n;
  1752. end;
  1753. function tcallnode.docompare(p: tnode): boolean;
  1754. begin
  1755. docompare :=
  1756. inherited docompare(p) and
  1757. (symtableprocentry = tcallnode(p).symtableprocentry) and
  1758. (procdefinition = tcallnode(p).procdefinition) and
  1759. { this implicitly also compares the vmt_entry node, as it is
  1760. deterministically based on the methodpointer }
  1761. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  1762. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  1763. (equal_defs(typedef,tcallnode(p).typedef))) or
  1764. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  1765. end;
  1766. {$ifdef DEBUG_NODE_XML}
  1767. procedure TCallNode.XMLPrintNodeData(var T: Text);
  1768. begin
  1769. if assigned(procdefinition) and (procdefinition.typ=procdef) then
  1770. WriteLn(T, PrintNodeIndention, '<procname>', SanitiseXMLString(TProcDef(procdefinition).FullProcName(True)), '</procname>')
  1771. else
  1772. begin
  1773. if assigned(symtableprocentry) then
  1774. WriteLn(T, PrintNodeIndention, '<procname>', symtableprocentry.name, '</procname>')
  1775. end;
  1776. if intrinsiccode <> Default(TInlineNumber) then
  1777. WriteLn(T, PrintNodeIndention, '<intrinsiccode>', intrinsiccode, '</intrinsiccode>');
  1778. if assigned(methodpointer) then
  1779. begin
  1780. WriteLn(T, PrintNodeIndention, '<methodpointer>');
  1781. PrintNodeIndent;
  1782. XMLPrintNode(T, methodpointer);
  1783. PrintNodeUnindent;
  1784. WriteLn(T, PrintNodeIndention, '</methodpointer>');
  1785. end;
  1786. if assigned(funcretnode) then
  1787. begin
  1788. WriteLn(T, PrintNodeIndention, '<funcretnode>');
  1789. PrintNodeIndent;
  1790. XMLPrintNode(T, funcretnode);
  1791. PrintNodeUnindent;
  1792. WriteLn(T, PrintNodeIndention, '</funcretnode>');
  1793. end;
  1794. if assigned(vmt_entry) then
  1795. begin
  1796. WriteLn(T, PrintNodeIndention, '<vmt_entry>');
  1797. PrintNodeIndent;
  1798. XMLPrintNode(T, vmt_entry);
  1799. PrintNodeUnindent;
  1800. WriteLn(T, PrintNodeIndention, '</vmt_entry>');
  1801. end;
  1802. if assigned(call_self_node) then
  1803. begin
  1804. WriteLn(T, PrintNodeIndention, '<call_self_node>');
  1805. PrintNodeIndent;
  1806. XMLPrintNode(T, call_self_node);
  1807. PrintNodeUnindent;
  1808. WriteLn(T, PrintNodeIndention, '</call_self_node>');
  1809. end;
  1810. if assigned(call_vmt_node) then
  1811. begin
  1812. WriteLn(T, PrintNodeIndention, '<call_vmt_node>');
  1813. PrintNodeIndent;
  1814. XMLPrintNode(T, call_vmt_node);
  1815. PrintNodeUnindent;
  1816. WriteLn(T, PrintNodeIndention, '</call_vmt_node>');
  1817. end;
  1818. if assigned(callinitblock) then
  1819. begin
  1820. WriteLn(T, PrintNodeIndention, '<callinitblock>');
  1821. PrintNodeIndent;
  1822. XMLPrintNode(T, callinitblock);
  1823. PrintNodeUnindent;
  1824. WriteLn(T, PrintNodeIndention, '</callinitblock>');
  1825. end;
  1826. if assigned(callcleanupblock) then
  1827. begin
  1828. WriteLn(T, PrintNodeIndention, '<callcleanupblock>');
  1829. PrintNodeIndent;
  1830. XMLPrintNode(T, callcleanupblock);
  1831. PrintNodeUnindent;
  1832. WriteLn(T, PrintNodeIndention, '</callcleanupblock>');
  1833. end;
  1834. inherited XMLPrintNodeData(T);
  1835. end;
  1836. {$endif DEBUG_NODE_XML}
  1837. procedure tcallnode.printnodedata(var t:text);
  1838. begin
  1839. if assigned(procdefinition) and
  1840. (procdefinition.typ=procdef) then
  1841. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  1842. else
  1843. begin
  1844. if assigned(symtableprocentry) then
  1845. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  1846. else
  1847. writeln(t,printnodeindention,'proc = <nil>');
  1848. end;
  1849. if intrinsiccode <> Default(TInlineNumber) then
  1850. writeln(t,printnodeindention,'intrinsiccode = ', intrinsiccode);
  1851. if assigned(methodpointer) then
  1852. begin
  1853. writeln(t,printnodeindention,'methodpointer =');
  1854. printnode(t,methodpointer);
  1855. end;
  1856. if assigned(funcretnode) then
  1857. begin
  1858. writeln(t,printnodeindention,'funcretnode =');
  1859. printnode(t,funcretnode);
  1860. end;
  1861. if assigned(vmt_entry) then
  1862. begin
  1863. writeln(t,printnodeindention,'vmt_entry =');
  1864. printnode(t,vmt_entry);
  1865. end;
  1866. if assigned(call_self_node) then
  1867. begin
  1868. writeln(t,printnodeindention,'call_self_node =');
  1869. printnode(t,call_self_node);
  1870. end;
  1871. if assigned(call_vmt_node) then
  1872. begin
  1873. writeln(t,printnodeindention,'call_vmt_node =');
  1874. printnode(t,call_vmt_node);
  1875. end;
  1876. if assigned(callinitblock) then
  1877. begin
  1878. writeln(t,printnodeindention,'callinitblock =');
  1879. printnode(t,callinitblock);
  1880. end;
  1881. if assigned(callcleanupblock) then
  1882. begin
  1883. writeln(t,printnodeindention,'callcleanupblock =');
  1884. printnode(t,callcleanupblock);
  1885. end;
  1886. if assigned(right) then
  1887. begin
  1888. writeln(t,printnodeindention,'right =');
  1889. printnode(t,right);
  1890. end;
  1891. if assigned(left) then
  1892. begin
  1893. writeln(t,printnodeindention,'left =');
  1894. printnode(t,left);
  1895. end;
  1896. end;
  1897. procedure tcallnode.insertintolist(l : tnodelist);
  1898. begin
  1899. end;
  1900. procedure tcallnode.add_init_statement(n:tnode);
  1901. var
  1902. lastinitstatement, before_firstpass : tstatementnode;
  1903. was_first_statement : boolean;
  1904. begin
  1905. if not assigned(n) then
  1906. exit;
  1907. if not assigned(callinitblock) then
  1908. begin
  1909. callinitblock:=internalstatements(lastinitstatement);
  1910. lastinitstatement.left.free;
  1911. lastinitstatement.left:=n;
  1912. firstpass(tnode(callinitblock));
  1913. exit;
  1914. end;
  1915. lastinitstatement:=laststatement(callinitblock);
  1916. was_first_statement:=(lastinitstatement=callinitblock.statements);
  1917. { all these nodes must be immediately typechecked, because this routine }
  1918. { can be called from pass_1 (i.e., after typecheck has already run) and }
  1919. { moreover, the entire blocks themselves are also only typechecked in }
  1920. { pass_1, while the the typeinfo is already required after the }
  1921. { typecheck pass for simplify purposes (not yet perfect, because the }
  1922. { statementnodes themselves are not typechecked this way) }
  1923. addstatement(lastinitstatement,n);
  1924. before_firstpass:=lastinitstatement;
  1925. firstpass(tnode(lastinitstatement));
  1926. if was_first_statement and (lastinitstatement<>before_firstpass) then
  1927. callinitblock.statements:=lastinitstatement;
  1928. { Update expectloc for callinitblock }
  1929. callinitblock.expectloc:=lastinitstatement.expectloc;
  1930. end;
  1931. procedure tcallnode.add_done_statement(n:tnode);
  1932. var
  1933. lastdonestatement, before_firstpass : tstatementnode;
  1934. was_first_statement : boolean;
  1935. begin
  1936. if not assigned(n) then
  1937. exit;
  1938. if not assigned(callcleanupblock) then
  1939. begin
  1940. callcleanupblock:=internalstatements(lastdonestatement);
  1941. lastdonestatement.left.free;
  1942. lastdonestatement.left:=n;
  1943. firstpass(tnode(callcleanupblock));
  1944. exit;
  1945. end;
  1946. lastdonestatement:=laststatement(callcleanupblock);
  1947. was_first_statement:=(lastdonestatement=callcleanupblock.statements);
  1948. { see comments in add_init_statement }
  1949. addstatement(lastdonestatement,n);
  1950. before_firstpass:=lastdonestatement;
  1951. firstpass(tnode(lastdonestatement));
  1952. if was_first_statement and (lastdonestatement<>before_firstpass) then
  1953. callcleanupblock.statements:=lastdonestatement;
  1954. { Update expectloc for callcleanupblock }
  1955. callcleanupblock.expectloc:=lastdonestatement.expectloc;
  1956. end;
  1957. function tcallnode.para_count:longint;
  1958. var
  1959. ppn : tcallparanode;
  1960. begin
  1961. result:=0;
  1962. ppn:=tcallparanode(left);
  1963. while assigned(ppn) do
  1964. begin
  1965. if not(assigned(ppn.parasym) and
  1966. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  1967. inc(result);
  1968. ppn:=tcallparanode(ppn.right);
  1969. end;
  1970. end;
  1971. function tcallnode.required_para_count: longint;
  1972. var
  1973. ppn : tcallparanode;
  1974. begin
  1975. result:=0;
  1976. ppn:=tcallparanode(left);
  1977. while assigned(ppn) do
  1978. begin
  1979. if not(assigned(ppn.parasym) and
  1980. ((vo_is_hidden_para in ppn.parasym.varoptions) or
  1981. assigned(ppn.parasym.defaultconstsym))) then
  1982. inc(result);
  1983. ppn:=tcallparanode(ppn.right);
  1984. end;
  1985. end;
  1986. function tcallnode.GetParaFromIndex(const Index: Integer): TCallParaNode;
  1987. var
  1988. hp : TCallParaNode;
  1989. Count: Integer;
  1990. begin
  1991. Result := nil;
  1992. Count := 0;
  1993. hp := TCallParaNode(left);
  1994. repeat
  1995. { If the original indices have not yet been set, just go by the order
  1996. they appear in the node tree }
  1997. if hp.originalindex = -1 then
  1998. begin
  1999. if Count = Index then
  2000. begin
  2001. Result := hp;
  2002. Exit;
  2003. end;
  2004. Inc(Count);
  2005. end
  2006. else if hp.originalindex = Index then
  2007. begin
  2008. Result := hp;
  2009. Exit;
  2010. end;
  2011. hp := TCallParaNode(hp.right);
  2012. until not Assigned(hp);
  2013. end;
  2014. function tcallnode.is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  2015. var
  2016. hp : tnode;
  2017. begin
  2018. hp:=p;
  2019. while assigned(hp) and
  2020. (hp.nodetype=typeconvn) and
  2021. (ttypeconvnode(hp).convtype=tc_equal) do
  2022. hp:=tunarynode(hp).left;
  2023. result:=(hp.nodetype in [typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn,addrn]);
  2024. if result and
  2025. not(may_be_in_reg) then
  2026. case hp.nodetype of
  2027. loadn:
  2028. result:=(tabstractvarsym(tloadnode(hp).symtableentry).varregable in [vr_none,vr_addr]);
  2029. temprefn:
  2030. result:=not(ti_may_be_in_reg in ttemprefnode(hp).tempflags);
  2031. else
  2032. ;
  2033. end;
  2034. end;
  2035. function tcallnode.getoverrideprocnamedef: tprocdef; inline;
  2036. begin
  2037. result:=foverrideprocnamedef;
  2038. end;
  2039. function look_for_call(var n: tnode; arg: pointer): foreachnoderesult;
  2040. begin
  2041. case n.nodetype of
  2042. calln,asn:
  2043. result := fen_norecurse_true;
  2044. typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn:
  2045. result := fen_norecurse_false;
  2046. else
  2047. result := fen_false;
  2048. end;
  2049. end;
  2050. procedure tcallnode.maybe_load_in_temp(var p:tnode);
  2051. begin
  2052. { Load all complex loads into a temp to prevent
  2053. double calls to a function. We can't simply check for a hp.nodetype=calln }
  2054. if assigned(p) and
  2055. foreachnodestatic(p,@look_for_call,nil) then
  2056. load_in_temp(p);
  2057. end;
  2058. procedure tcallnode.load_in_temp(var p:tnode);
  2059. var
  2060. actnode : pnode;
  2061. loadp,
  2062. refp : tnode;
  2063. hdef : tdef;
  2064. ptemp : ttempcreatenode;
  2065. usederef : boolean;
  2066. begin
  2067. if assigned(p) then
  2068. begin
  2069. { if the node is a deref node we load the pointer in a temp to allow
  2070. code using this node to still be able to modify the original
  2071. reference (e.g. a function returning a floating point value on x86
  2072. would pass that value through the FP stack and then to the stack
  2073. and thus e.g. a type helper for float called on that would modify
  2074. the temporary memory on the stack instead of the returned pointer
  2075. value }
  2076. actnode:=@p;
  2077. actnode:=actualtargetnode(actnode);
  2078. if actnode^.nodetype=derefn then
  2079. begin
  2080. load_in_temp(tderefnode(actnode^).left);
  2081. exit;
  2082. end;
  2083. { temp create }
  2084. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  2085. is_shortstring(p.resultdef) or
  2086. is_object(p.resultdef);
  2087. if usederef then
  2088. hdef:=cpointerdef.getreusable(p.resultdef)
  2089. else
  2090. hdef:=p.resultdef;
  2091. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  2092. if usederef then
  2093. begin
  2094. loadp:=caddrnode.create_internal(p);
  2095. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  2096. end
  2097. else
  2098. begin
  2099. loadp:=p;
  2100. refp:=ctemprefnode.create(ptemp);
  2101. { ensure that an invokable isn't called again }
  2102. if is_invokable(hdef) then
  2103. include(ttemprefnode(refp).flags,nf_load_procvar);
  2104. end;
  2105. add_init_statement(ptemp);
  2106. add_init_statement(cassignmentnode.create(
  2107. ctemprefnode.create(ptemp),
  2108. loadp));
  2109. add_done_statement(ctempdeletenode.create(ptemp));
  2110. { new tree is only a temp reference }
  2111. p:=refp;
  2112. typecheckpass(p);
  2113. end;
  2114. end;
  2115. function tcallnode.gen_high_tree(var p:tnode;paradef:tdef):tnode;
  2116. { When passing an array to an open array, or a string to an open string,
  2117. some code is needed that generates the high bound of the array. This
  2118. function returns a tree containing the nodes for it. }
  2119. var
  2120. temp: tnode;
  2121. len : integer;
  2122. loadconst : boolean;
  2123. hightree,l,r : tnode;
  2124. defkind: tdeftyp;
  2125. begin
  2126. len:=-1;
  2127. loadconst:=true;
  2128. hightree:=nil;
  2129. { constant strings are internally stored as array of char, but if the
  2130. parameter is a string also treat it like one }
  2131. defkind:=p.resultdef.typ;
  2132. if (p.nodetype=stringconstn) and
  2133. (paradef.typ=stringdef) then
  2134. defkind:=stringdef;
  2135. case defkind of
  2136. arraydef :
  2137. begin
  2138. if (paradef.typ<>arraydef) then
  2139. internalerror(200405241);
  2140. { passing a string to an array of char }
  2141. if (p.nodetype=stringconstn) and
  2142. is_char(tarraydef(paradef).elementdef) then
  2143. begin
  2144. len:=tstringconstnode(p).len;
  2145. if len>0 then
  2146. dec(len);
  2147. end
  2148. else
  2149. { handle special case of passing an single array to an array of array }
  2150. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  2151. len:=0
  2152. else
  2153. begin
  2154. { handle via a normal inline in_high_x node }
  2155. loadconst:=false;
  2156. { slice? }
  2157. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  2158. with Tcallparanode(Tinlinenode(p).left) do
  2159. begin
  2160. {Array slice using slice builtin function.}
  2161. l:=Tcallparanode(right).left;
  2162. hightree:=caddnode.create(subn,geninlinenode(in_ord_x,false,l),genintconstnode(1));
  2163. Tcallparanode(right).left:=nil;
  2164. {Remove the inline node.}
  2165. temp:=p;
  2166. p:=left;
  2167. Tcallparanode(tinlinenode(temp).left).left:=nil;
  2168. temp.free;
  2169. typecheckpass(hightree);
  2170. end
  2171. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  2172. begin
  2173. {Array slice using .. operator.}
  2174. with Trangenode(Tvecnode(p).right) do
  2175. begin
  2176. l:=geninlinenode(in_ord_x,false,left); {Get lower bound.}
  2177. r:=geninlinenode(in_ord_x,false,right); {Get upper bound.}
  2178. end;
  2179. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  2180. hightree:=caddnode.create(subn,r,l);
  2181. {Replace the rangnode in the tree by its lower_bound, and
  2182. dispose the rangenode.}
  2183. temp:=Tvecnode(p).right;
  2184. Tvecnode(p).right:=l.getcopy;
  2185. {Typecheckpass can only be performed *after* the l.getcopy since it
  2186. can modify the tree, and l is in the hightree.}
  2187. typecheckpass(hightree);
  2188. with Trangenode(temp) do
  2189. begin
  2190. left:=nil;
  2191. right:=nil;
  2192. end;
  2193. temp.free;
  2194. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  2195. p.resultdef:=nil;
  2196. typecheckpass(p);
  2197. end
  2198. else
  2199. begin
  2200. maybe_load_in_temp(p);
  2201. hightree:=geninlinenode(in_ord_x,false,geninlinenode(in_high_x,false,p.getcopy));
  2202. typecheckpass(hightree);
  2203. { only substract low(array) if it's <> 0 }
  2204. temp:=geninlinenode(in_ord_x,false,geninlinenode(in_low_x,false,p.getcopy));
  2205. typecheckpass(temp);
  2206. if (temp.nodetype <> ordconstn) or
  2207. (tordconstnode(temp).value <> 0) then
  2208. begin
  2209. hightree:=caddnode.create(subn,hightree,temp);
  2210. include(hightree.flags,nf_internal);
  2211. end
  2212. else
  2213. temp.free;
  2214. end;
  2215. end;
  2216. end;
  2217. stringdef :
  2218. begin
  2219. if is_open_string(paradef) then
  2220. begin
  2221. { a stringconstn is not a simple parameter and hence would be
  2222. loaded in a temp, but in that case the high() node
  2223. a) goes wrong (it cannot deal with a temp node)
  2224. b) would give a generic result instead of one specific to
  2225. this constant string
  2226. }
  2227. if p.nodetype<>stringconstn then
  2228. maybe_load_in_temp(p);
  2229. { handle via a normal inline in_high_x node }
  2230. loadconst := false;
  2231. hightree := geninlinenode(in_high_x,false,p.getcopy);
  2232. end
  2233. else
  2234. { handle special case of passing an single string to an array of string }
  2235. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  2236. len:=0
  2237. else
  2238. { passing a string to an array of char }
  2239. if (p.nodetype=stringconstn) and
  2240. is_char(tarraydef(paradef).elementdef) then
  2241. begin
  2242. len:=tstringconstnode(p).len;
  2243. if len>0 then
  2244. dec(len);
  2245. end
  2246. else
  2247. begin
  2248. maybe_load_in_temp(p);
  2249. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  2250. cordconstnode.create(1,sizesinttype,false));
  2251. loadconst:=false;
  2252. end;
  2253. end;
  2254. else
  2255. len:=0;
  2256. end;
  2257. if loadconst then
  2258. hightree:=cordconstnode.create(len,sizesinttype,true)
  2259. else
  2260. begin
  2261. if not assigned(hightree) then
  2262. internalerror(200304071);
  2263. { Need to use explicit, because it can also be a enum }
  2264. hightree:=ctypeconvnode.create_internal(hightree,sizesinttype);
  2265. end;
  2266. result:=hightree;
  2267. end;
  2268. function tcallnode.gen_procvar_context_tree_self:tnode;
  2269. begin
  2270. { Load tmehodpointer(right).self }
  2271. result:=genloadfield(ctypeconvnode.create_internal(
  2272. right.getcopy,methodpointertype),
  2273. 'self');
  2274. end;
  2275. function tcallnode.gen_procvar_context_tree_parentfp: tnode;
  2276. begin
  2277. { Load tnestedprocpointer(right).parentfp }
  2278. result:=genloadfield(ctypeconvnode.create_internal(
  2279. right.getcopy,nestedprocpointertype),
  2280. 'parentfp');
  2281. end;
  2282. function tcallnode.gen_self_tree:tnode;
  2283. var
  2284. selftree : tnode;
  2285. selfdef : tdef;
  2286. temp : ttempcreatenode;
  2287. begin
  2288. selftree:=nil;
  2289. { When methodpointer was a callnode we must load it first into a
  2290. temp to prevent processing the callnode twice }
  2291. if (methodpointer.nodetype=calln) then
  2292. internalerror(200405121);
  2293. { Objective-C: objc_convert_to_message_send() already did all necessary
  2294. transformation on the methodpointer }
  2295. if (procdefinition.typ=procdef) and
  2296. (po_objc in tprocdef(procdefinition).procoptions) then
  2297. selftree:=methodpointer.getcopy
  2298. { inherited }
  2299. else if (cnf_inherited in callnodeflags) then
  2300. begin
  2301. selftree:=safe_call_self_node.getcopy;
  2302. { we can call an inherited class static/method from a regular method
  2303. -> self node must change from instance pointer to vmt pointer)
  2304. }
  2305. if (procdefinition.procoptions*[po_classmethod,po_staticmethod] <> []) and
  2306. (selftree.resultdef.typ<>classrefdef) then
  2307. selftree:=cloadvmtaddrnode.create(selftree);
  2308. end
  2309. else
  2310. { constructors }
  2311. if (procdefinition.proctypeoption=potype_constructor) then
  2312. begin
  2313. if (methodpointer.resultdef.typ=classrefdef) or
  2314. (cnf_new_call in callnodeflags) then
  2315. if not is_javaclass(tdef(procdefinition.owner.defowner)) then
  2316. begin
  2317. if (cnf_new_call in callnodeflags) then
  2318. { old-style object: push 0 as self }
  2319. selftree:=cpointerconstnode.create(0,voidpointertype)
  2320. else
  2321. begin
  2322. { class-style: push classtype }
  2323. selftree:=methodpointer.getcopy;
  2324. if selftree.nodetype=typen then
  2325. begin
  2326. selftree:=cloadvmtaddrnode.create(selftree);
  2327. tloadvmtaddrnode(selftree).forcall:=true;
  2328. end;
  2329. end;
  2330. end
  2331. else
  2332. { special handling for Java constructors, handled in
  2333. tjvmcallnode.extra_pre_call_code }
  2334. selftree:=cnothingnode.create
  2335. else
  2336. begin
  2337. if methodpointer.nodetype=typen then
  2338. if (methodpointer.resultdef.typ<>objectdef) then
  2339. begin
  2340. if not(target_info.system in systems_jvm) then
  2341. begin
  2342. { TSomeRecord.Constructor call. We need to allocate }
  2343. { self node as a temp node of the result type }
  2344. temp:=ctempcreatenode.create(methodpointer.resultdef,methodpointer.resultdef.size,tt_persistent,false);
  2345. add_init_statement(temp);
  2346. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2347. selftree:=ctemprefnode.create(temp);
  2348. end
  2349. else
  2350. begin
  2351. { special handling for Java constructors, handled in
  2352. tjvmcallnode.extra_pre_call_code }
  2353. selftree:=cnothingnode.create
  2354. end;
  2355. end
  2356. else
  2357. selftree:=safe_call_self_node.getcopy
  2358. else
  2359. selftree:=methodpointer.getcopy;
  2360. end;
  2361. end
  2362. else
  2363. { Calling a static/class method }
  2364. if (po_classmethod in procdefinition.procoptions) or
  2365. (po_staticmethod in procdefinition.procoptions) then
  2366. begin
  2367. if (procdefinition.typ<>procdef) then
  2368. internalerror(200305062);
  2369. { if the method belongs to a helper then we need to use the
  2370. extended type for references to Self }
  2371. if is_objectpascal_helper(tprocdef(procdefinition).struct) then
  2372. selfdef:=tobjectdef(tprocdef(procdefinition).struct).extendeddef
  2373. else
  2374. selfdef:=tprocdef(procdefinition).struct;
  2375. if ((selfdef.typ in [recorddef,objectdef]) and
  2376. (oo_has_vmt in tabstractrecorddef(selfdef).objectoptions)) or
  2377. { all Java classes have a "VMT" }
  2378. (target_info.system in systems_jvm) then
  2379. begin
  2380. { we only need the vmt, loading self is not required and there is no
  2381. need to check for typen, because that will always get the
  2382. loadvmtaddrnode added }
  2383. selftree:=methodpointer.getcopy;
  2384. if (methodpointer.resultdef.typ<>classrefdef) or
  2385. (methodpointer.nodetype = typen) then
  2386. selftree:=cloadvmtaddrnode.create(selftree);
  2387. end
  2388. else
  2389. selftree:=cpointerconstnode.create(0,voidpointertype);
  2390. end
  2391. else
  2392. begin
  2393. if methodpointer.nodetype=typen then
  2394. selftree:=safe_call_self_node.getcopy
  2395. else
  2396. selftree:=methodpointer.getcopy;
  2397. end;
  2398. result:=selftree;
  2399. end;
  2400. function tcallnode.use_caller_self(check_for_callee_self: boolean): boolean;
  2401. var
  2402. i: longint;
  2403. ps: tparavarsym;
  2404. begin
  2405. result:=false;
  2406. { is there a self parameter? }
  2407. if check_for_callee_self then
  2408. begin
  2409. ps:=nil;
  2410. for i:=0 to procdefinition.paras.count-1 do
  2411. begin
  2412. ps:=tparavarsym(procdefinition.paras[i]);
  2413. if vo_is_self in ps.varoptions then
  2414. break;
  2415. ps:=nil;
  2416. end;
  2417. if not assigned(ps) then
  2418. exit;
  2419. end;
  2420. { we need to load the'self' parameter of the current routine as the
  2421. 'self' parameter of the called routine if
  2422. 1) we're calling an inherited routine
  2423. 2) we're calling a constructor via type.constructorname and
  2424. type is not a classrefdef (i.e., we're calling a constructor like
  2425. a regular method)
  2426. 3) we're calling any regular (non-class/non-static) method via
  2427. a typenode (the methodpointer is then that typenode, but the
  2428. passed self node must become the current self node)
  2429. In other cases, we either don't have to pass the 'self' parameter of
  2430. the current routine to the called one, or methodpointer will already
  2431. contain it (e.g. because a method was called via "method", in which
  2432. case the parser already passed 'self' as the method pointer, or via
  2433. "self.method") }
  2434. if (cnf_inherited in callnodeflags) or
  2435. ((procdefinition.proctypeoption=potype_constructor) and
  2436. not((methodpointer.resultdef.typ=classrefdef) or
  2437. (cnf_new_call in callnodeflags)) and
  2438. (methodpointer.nodetype=typen) and
  2439. (methodpointer.resultdef.typ=objectdef)) or
  2440. (assigned(methodpointer) and
  2441. (procdefinition.proctypeoption<>potype_constructor) and
  2442. not(po_classmethod in procdefinition.procoptions) and
  2443. not(po_staticmethod in procdefinition.procoptions) and
  2444. (methodpointer.nodetype=typen)) then
  2445. result:=true;
  2446. end;
  2447. procedure tcallnode.maybe_gen_call_self_node;
  2448. begin
  2449. if cnf_call_self_node_done in callnodeflags then
  2450. exit;
  2451. include(callnodeflags,cnf_call_self_node_done);
  2452. if use_caller_self(true) then
  2453. call_self_node:=load_self_node;
  2454. end;
  2455. procedure tcallnode.register_created_object_types;
  2456. var
  2457. crefdef,
  2458. systobjectdef : tdef;
  2459. begin
  2460. { only makes sense for methods }
  2461. if not assigned(methodpointer) then
  2462. exit;
  2463. { inherited calls don't create an instance of the inherited type, but of
  2464. the current type }
  2465. if ([cnf_inherited,cnf_anon_inherited,cnf_ignore_devirt_wpo]*callnodeflags)<>[] then
  2466. exit;
  2467. if (methodpointer.resultdef.typ=classrefdef) then
  2468. begin
  2469. { constructor call via classreference => instance can be created
  2470. same with calling newinstance without a instance-self (don't
  2471. consider self-based newinstance calls, because then everything
  2472. will be assumed to be just a TObject since TObject.Create calls
  2473. NewInstance) }
  2474. if procdefinition.wpo_may_create_instance(methodpointer) then
  2475. begin
  2476. { Only a typenode can be passed when it is called with <class of xx>.create }
  2477. if (methodpointer.nodetype=typen) then
  2478. begin
  2479. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2480. { we know the exact class type being created }
  2481. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2482. end
  2483. else
  2484. begin
  2485. { the loadvmtaddrnode is already created in case of classtype.create }
  2486. if (methodpointer.nodetype=loadvmtaddrn) and
  2487. (tloadvmtaddrnode(methodpointer).left.nodetype=typen) then
  2488. begin
  2489. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2490. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2491. end
  2492. else
  2493. begin
  2494. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2495. begin
  2496. { special case: if the classref comes from x.classtype (with classtype,
  2497. being tobject.classtype) then the created instance is x or a descendant
  2498. of x (rather than tobject or a descendant of tobject)
  2499. }
  2500. systobjectdef:=search_system_type('TOBJECT').typedef;
  2501. if (methodpointer.nodetype=calln) and
  2502. { not a procvar call }
  2503. not assigned(right) and
  2504. { procdef is owned by system.tobject }
  2505. (tprocdef(tcallnode(methodpointer).procdefinition).owner.defowner=systobjectdef) and
  2506. { we're calling system.tobject.classtype }
  2507. (tcallnode(methodpointer).symtableprocentry.name='CLASSTYPE') and
  2508. { could again be a classrefdef, but unlikely }
  2509. (tcallnode(methodpointer).methodpointer.resultdef.typ=objectdef) and
  2510. { don't go through this trouble if it was already a tobject }
  2511. (tcallnode(methodpointer).methodpointer.resultdef<>systobjectdef) then
  2512. begin
  2513. { register this object type as classref, so all descendents will also
  2514. be marked as instantiatable (only the pointeddef will actually be
  2515. recorded, so it's no problem that the clasrefdef is only temporary)
  2516. }
  2517. crefdef:=cclassrefdef.create(tcallnode(methodpointer).methodpointer.resultdef);
  2518. { and register it }
  2519. crefdef.register_created_object_type;
  2520. end
  2521. else
  2522. { the created class can be any child class as well -> register classrefdef }
  2523. methodpointer.resultdef.register_created_object_type;
  2524. end;
  2525. end;
  2526. end;
  2527. end
  2528. end
  2529. else
  2530. { Old style object }
  2531. if is_object(methodpointer.resultdef) then
  2532. begin
  2533. { constructor with extended syntax called from new }
  2534. if (cnf_new_call in callnodeflags) then
  2535. begin
  2536. if wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2537. methodpointer.resultdef.register_created_object_type;
  2538. end
  2539. else
  2540. { normal object call like obj.proc }
  2541. if not(cnf_dispose_call in callnodeflags) and
  2542. not(cnf_inherited in callnodeflags) and
  2543. not(cnf_member_call in callnodeflags) then
  2544. begin
  2545. if (procdefinition.proctypeoption=potype_constructor) then
  2546. begin
  2547. if (methodpointer.nodetype<>typen) and
  2548. wpoinfomanager.symbol_live_in_currentproc(methodpointer.resultdef) then
  2549. methodpointer.resultdef.register_created_object_type;
  2550. end
  2551. end;
  2552. end;
  2553. end;
  2554. function tcallnode.get_expect_loc: tcgloc;
  2555. var
  2556. realresdef: tstoreddef;
  2557. begin
  2558. if not assigned(typedef) then
  2559. realresdef:=tstoreddef(resultdef)
  2560. else
  2561. realresdef:=tstoreddef(typedef);
  2562. if realresdef.is_intregable then
  2563. result:=LOC_REGISTER
  2564. else if (realresdef.typ=floatdef) and
  2565. not(cs_fp_emulation in current_settings.moduleswitches) then
  2566. if use_vectorfpu(realresdef) then
  2567. result:=LOC_MMREGISTER
  2568. else
  2569. {$ifdef x86}
  2570. result:=LOC_REFERENCE
  2571. {$else x86}
  2572. result:=LOC_FPUREGISTER
  2573. {$endif x86}
  2574. else
  2575. result:=LOC_REFERENCE
  2576. end;
  2577. function tcallnode.handle_compilerproc: tnode;
  2578. var
  2579. para: TCallParaNode;
  2580. maxlennode, outnode, valnode: TNode;
  2581. MaxStrLen: Int64;
  2582. StringLiteral, name: string;
  2583. ValOutput: TConstExprInt;
  2584. ValCode: Longint;
  2585. NewStatements: TStatementNode;
  2586. si : ShortInt;
  2587. b: Byte;
  2588. i: SmallInt;
  2589. w: Word;
  2590. li: LongInt;
  2591. dw: DWord;
  2592. i64: Int64;
  2593. qw: QWord;
  2594. begin
  2595. result := nil;
  2596. case intrinsiccode of
  2597. in_str_x_string:
  2598. begin
  2599. { rare optimization opportunity which takes some extra time,
  2600. so check only at level 3+ }
  2601. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  2602. exit;
  2603. { If n is a constant, attempt to convert, for example:
  2604. "Str(5, Output);" to "Output := '5';" }
  2605. { Format of the internal function (also for fpc_shortstr_uint) is:
  2606. $fpc_shortstr_sint(Int64;Int64;out OpenString;<const Int64>); }
  2607. { Remember the parameters are in reverse order - the leftmost one
  2608. can usually be ignored }
  2609. para := GetParaFromIndex(1);
  2610. if Assigned(para) then
  2611. begin
  2612. { Output variable }
  2613. outnode := para.left;
  2614. para := GetParaFromIndex(2);
  2615. if Assigned(para) then
  2616. begin
  2617. { Maximum length }
  2618. maxlennode := para.left;
  2619. if is_integer(maxlennode.resultdef) then
  2620. begin
  2621. para := GetParaFromIndex(3);
  2622. while (maxlennode.nodetype = typeconvn) and (ttypeconvnode(maxlennode).convtype in [tc_equal, tc_int_2_int]) do
  2623. begin
  2624. maxlennode := ttypeconvnode(maxlennode).left;
  2625. end;
  2626. if Assigned(para) and is_constintnode(maxlennode) then
  2627. begin
  2628. { Numeric value }
  2629. valnode := para.left;
  2630. if is_integer(valnode.resultdef) and not Assigned(GetParaFromIndex(4)) then
  2631. begin
  2632. while (valnode.nodetype = typeconvn) and (ttypeconvnode(valnode).convtype in [tc_equal, tc_int_2_int]) do
  2633. begin
  2634. valnode := ttypeconvnode(valnode).left;
  2635. end;
  2636. if is_constintnode(valnode) then
  2637. begin
  2638. MaxStrLen := TOrdConstNode(maxlennode).value.svalue;
  2639. { If we've gotten this far, we can convert the node into a direct assignment }
  2640. StringLiteral := tostr(tordconstnode(valnode).value);
  2641. if MaxStrLen <> -1 then
  2642. SetLength(StringLiteral, Integer(MaxStrLen));
  2643. result := cassignmentnode.create(
  2644. outnode.getcopy,
  2645. cstringconstnode.createstr(StringLiteral)
  2646. );
  2647. end;
  2648. end;
  2649. end;
  2650. end;
  2651. end;
  2652. end;
  2653. end;
  2654. in_val_x:
  2655. begin
  2656. { rare optimization opportunity which takes some extra time,
  2657. so check only at level 3+ }
  2658. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  2659. exit;
  2660. { If the input is a constant, attempt to convert, for example:
  2661. "Val('5', Output, Code);" to "Output := 5; Code := 0;" }
  2662. { Format of the internal function fpc_val_sint_*str) is:
  2663. fpc_val_sint_*str(SizeInt; *String; out ValSInt): ValSInt; }
  2664. { Remember the parameters are in reverse order - the leftmost one
  2665. is the integer data size can usually be ignored.
  2666. For fpc_val_uint_*str variants, the data size is not present as
  2667. of FPC 3.2.0
  2668. Para indices:
  2669. * 0 = Code output (present even if omitted in original code)
  2670. * 1 = String input
  2671. * 2 = Data size
  2672. }
  2673. para := GetParaFromIndex(0);
  2674. if Assigned(para) then
  2675. begin
  2676. outnode := para.left;
  2677. para := GetParaFromIndex(1);
  2678. if Assigned(para) then
  2679. begin
  2680. valnode:=para.left;
  2681. name:=tprocdef(procdefinition).fullprocname(true);
  2682. if is_conststringnode(valnode) and
  2683. { we can handle only the fpc_val_sint helpers so far }
  2684. ((copy(name,1,13)='$fpc_val_sint') or (copy(name,1,13)='$fpc_val_uint')) then
  2685. begin
  2686. ValOutput.signed := is_signed(ResultDef);
  2687. case Longint(tordconstnode(GetParaFromIndex(2).paravalue).value.svalue) of
  2688. 1:
  2689. if ValOutput.signed then
  2690. begin
  2691. Val(TStringConstNode(valnode).asrawbytestring, si, ValCode);
  2692. ValOutput.svalue:=si;
  2693. end
  2694. else
  2695. begin
  2696. Val(TStringConstNode(valnode).asrawbytestring, b, ValCode);
  2697. ValOutput.uvalue:=b;
  2698. end;
  2699. 2:
  2700. if ValOutput.signed then
  2701. begin
  2702. Val(TStringConstNode(valnode).asrawbytestring, i, ValCode);
  2703. ValOutput.svalue:=i;
  2704. end
  2705. else
  2706. begin
  2707. Val(TStringConstNode(valnode).asrawbytestring, w, ValCode);
  2708. ValOutput.uvalue:=w;
  2709. end;
  2710. 4:
  2711. if ValOutput.signed then
  2712. begin
  2713. Val(TStringConstNode(valnode).asrawbytestring, li, ValCode);
  2714. ValOutput.svalue:=li;
  2715. end
  2716. else
  2717. begin
  2718. Val(TStringConstNode(valnode).asrawbytestring, dw, ValCode);
  2719. ValOutput.uvalue:=dw;
  2720. end;
  2721. 8:
  2722. if ValOutput.signed then
  2723. begin
  2724. Val(TStringConstNode(valnode).asrawbytestring, i64, ValCode);
  2725. ValOutput.svalue:=i64;
  2726. end
  2727. else
  2728. begin
  2729. Val(TStringConstNode(valnode).asrawbytestring, qw, ValCode);
  2730. ValOutput.uvalue:=qw;
  2731. end;
  2732. else
  2733. Internalerror(2024011402);
  2734. end;
  2735. { Due to the way the node tree works, we have to insert
  2736. the assignment to the Code output within the
  2737. assignment to the value output (function result),
  2738. so use a block node for that}
  2739. Result := internalstatements(NewStatements);
  2740. { Create a node for writing the Code output }
  2741. addstatement(
  2742. NewStatements,
  2743. CAssignmentNode.Create_Internal(
  2744. outnode.getcopy(), { The original will get destroyed }
  2745. COrdConstNode.Create(ValCode, outnode.ResultDef, False)
  2746. )
  2747. );
  2748. { Now actually create the function result }
  2749. case resultdef.typ of
  2750. orddef:
  2751. valnode := COrdConstNode.Create(ValOutput, resultdef, False);
  2752. else
  2753. Internalerror(2024011401);
  2754. end;
  2755. addstatement(NewStatements, valnode);
  2756. { Result will now undergo firstpass }
  2757. end;
  2758. end;
  2759. end;
  2760. end;
  2761. else
  2762. ;
  2763. end;
  2764. end;
  2765. function tcallnode.safe_call_self_node: tnode;
  2766. begin
  2767. if not assigned(call_self_node) then
  2768. begin
  2769. CGMessage(parser_e_illegal_expression);
  2770. call_self_node:=cerrornode.create;
  2771. end;
  2772. result:=call_self_node;
  2773. end;
  2774. procedure tcallnode.gen_vmt_entry_load;
  2775. var
  2776. vmt_def: trecorddef;
  2777. begin
  2778. if not assigned(right) and
  2779. not assigned(overrideprocnamedef) and
  2780. (po_virtualmethod in procdefinition.procoptions) and
  2781. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  2782. assigned(methodpointer) and
  2783. (methodpointer.nodetype<>typen) then
  2784. begin
  2785. vmt_entry:=load_vmt_for_self_node(methodpointer.getcopy);
  2786. { get the right entry in the VMT }
  2787. vmt_entry:=cderefnode.create(vmt_entry);
  2788. typecheckpass(vmt_entry);
  2789. vmt_def:=trecorddef(vmt_entry.resultdef);
  2790. { tobjectdef(tprocdef(procdefinition).struct) can be a parent of the
  2791. methodpointer's resultdef, but the vmtmethodoffset of the method
  2792. in that objectdef is obviously the same as in any child class }
  2793. vmt_entry:=csubscriptnode.create(
  2794. trecordsymtable(vmt_def.symtable).findfieldbyoffset(
  2795. tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber)
  2796. ),
  2797. vmt_entry
  2798. );
  2799. firstpass(vmt_entry);
  2800. end;
  2801. end;
  2802. procedure tcallnode.gen_syscall_para(para: tcallparanode);
  2803. begin
  2804. { unsupported }
  2805. internalerror(2014040101);
  2806. end;
  2807. procedure tcallnode.objc_convert_to_message_send;
  2808. var
  2809. block,
  2810. selftree : tnode;
  2811. statements : tstatementnode;
  2812. field : tfieldvarsym;
  2813. temp : ttempcreatenode;
  2814. selfrestype,
  2815. objcsupertype : tdef;
  2816. srsym : tsym;
  2817. srsymtable : tsymtable;
  2818. msgsendname : string;
  2819. begin
  2820. if not(m_objectivec1 in current_settings.modeswitches) then
  2821. Message(parser_f_modeswitch_objc_required);
  2822. { typecheck pass must already have run on the call node,
  2823. because pass1 calls this method
  2824. }
  2825. { default behaviour: call objc_msgSend and friends;
  2826. 64 bit targets for Mac OS X can override this as they
  2827. can call messages via an indirect function call similar to
  2828. dynamically linked functions, ARM maybe as well (not checked)
  2829. Which variant of objc_msgSend is used depends on the
  2830. result type, and on whether or not it's an inherited call.
  2831. }
  2832. { make sure we don't perform this transformation twice in case
  2833. firstpass would be called multiple times }
  2834. include(callnodeflags,cnf_objc_processed);
  2835. { make sure the methodpointer doesn't get translated into a call
  2836. as well (endless loop) }
  2837. if methodpointer.nodetype=loadvmtaddrn then
  2838. tloadvmtaddrnode(methodpointer).forcall:=true;
  2839. { A) set the appropriate objc_msgSend* variant to call }
  2840. { The AArch64 abi does not require special handling for struct returns }
  2841. {$ifndef aarch64}
  2842. { record returned via implicit pointer }
  2843. if paramanager.ret_in_param(resultdef,procdefinition) then
  2844. begin
  2845. if not(cnf_inherited in callnodeflags) then
  2846. msgsendname:='OBJC_MSGSEND_STRET'
  2847. else if (target_info.system in systems_objc_nfabi) and
  2848. (not MacOSXVersionMin.isvalid or
  2849. (MacOSXVersionMin.relationto(10,6,0)>=0)) then
  2850. msgsendname:='OBJC_MSGSENDSUPER2_STRET'
  2851. else
  2852. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  2853. end
  2854. {$ifdef i386}
  2855. { special case for fpu results on i386 for non-inherited calls }
  2856. { TODO: also for x86_64 "extended" results }
  2857. else if (resultdef.typ=floatdef) and
  2858. not(cnf_inherited in callnodeflags) then
  2859. msgsendname:='OBJC_MSGSEND_FPRET'
  2860. {$endif i386}
  2861. { default }
  2862. else
  2863. {$endif aarch64}
  2864. if not(cnf_inherited in callnodeflags) then
  2865. msgsendname:='OBJC_MSGSEND'
  2866. else if (target_info.system in systems_objc_nfabi) and
  2867. (not MacOSXVersionMin.isvalid or
  2868. (MacOSXVersionMin.relationto(10,6,0)>=0)) then
  2869. msgsendname:='OBJC_MSGSENDSUPER2'
  2870. else
  2871. msgsendname:='OBJC_MSGSENDSUPER';
  2872. { get the mangled name }
  2873. srsym:=nil;
  2874. if not searchsym_in_named_module('OBJC',msgsendname,srsym,srsymtable) or
  2875. (srsym.typ<>procsym) or
  2876. (tprocsym(srsym).ProcdefList.count<>1) then
  2877. Message1(cg_f_unknown_compilerproc,'objc.'+msgsendname);
  2878. foverrideprocnamedef:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  2879. { B) Handle self }
  2880. { 1) in case of sending a message to a superclass, self is a pointer to
  2881. an objc_super record
  2882. }
  2883. if (cnf_inherited in callnodeflags) then
  2884. begin
  2885. block:=internalstatements(statements);
  2886. objcsupertype:=search_named_unit_globaltype('OBJC','OBJC_SUPER',true).typedef;
  2887. if (objcsupertype.typ<>recorddef) then
  2888. internalerror(2009032901);
  2889. { temp for the for the objc_super record }
  2890. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  2891. addstatement(statements,temp);
  2892. { initialize objc_super record }
  2893. selftree:=safe_call_self_node.getcopy;
  2894. { we can call an inherited class static/method from a regular method
  2895. -> self node must change from instance pointer to vmt pointer)
  2896. }
  2897. if (po_classmethod in procdefinition.procoptions) and
  2898. (selftree.resultdef.typ<>classrefdef) then
  2899. begin
  2900. selftree:=cloadvmtaddrnode.create(selftree);
  2901. { since we're in a class method of the current class, its
  2902. information has already been initialized (and that of all of
  2903. its parent classes too) }
  2904. tloadvmtaddrnode(selftree).forcall:=true;
  2905. typecheckpass(selftree);
  2906. end;
  2907. selfrestype:=selftree.resultdef;
  2908. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  2909. if not assigned(field) then
  2910. internalerror(2009032902);
  2911. { first the destination object/class instance }
  2912. addstatement(statements,
  2913. cassignmentnode.create(
  2914. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2915. selftree
  2916. )
  2917. );
  2918. { and secondly, the class type in which the selector must be looked
  2919. up (the parent class in case of an instance method, the parent's
  2920. metaclass in case of a class method) }
  2921. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  2922. if not assigned(field) then
  2923. internalerror(2009032903);
  2924. addstatement(statements,
  2925. cassignmentnode.create(
  2926. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2927. objcsuperclassnode(selftree.resultdef)
  2928. )
  2929. );
  2930. { result of this block is the address of this temp }
  2931. addstatement(statements,ctypeconvnode.create_internal(
  2932. caddrnode.create_internal(ctemprefnode.create(temp)),selfrestype)
  2933. );
  2934. { replace the method pointer with the address of this temp }
  2935. methodpointer.free;
  2936. methodpointer:=block;
  2937. typecheckpass(block);
  2938. end
  2939. else
  2940. { 2) regular call (not inherited) }
  2941. begin
  2942. { a) If we're calling a class method, use a class ref. }
  2943. if (po_classmethod in procdefinition.procoptions) and
  2944. ((methodpointer.nodetype=typen) or
  2945. (methodpointer.resultdef.typ<>classrefdef)) then
  2946. begin
  2947. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  2948. { no need to obtain the class ref by calling class(), sending
  2949. this message will initialize it if necessary }
  2950. tloadvmtaddrnode(methodpointer).forcall:=true;
  2951. firstpass(methodpointer);
  2952. end;
  2953. end;
  2954. end;
  2955. function tcallnode.gen_vmt_tree:tnode;
  2956. var
  2957. vmttree : tnode;
  2958. begin
  2959. vmttree:=nil;
  2960. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  2961. internalerror(200305051);
  2962. { When methodpointer was a callnode we must load it first into a
  2963. temp to prevent the processing callnode twice }
  2964. if (methodpointer.nodetype=calln) then
  2965. internalerror(200405122);
  2966. { Handle classes and legacy objects separate to make it
  2967. more maintainable }
  2968. if (methodpointer.resultdef.typ=classrefdef) then
  2969. begin
  2970. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  2971. internalerror(200501041);
  2972. { constructor call via classreference => allocate memory }
  2973. if (procdefinition.proctypeoption=potype_constructor) then
  2974. begin
  2975. vmttree:=cpointerconstnode.create(1,voidpointertype);
  2976. end
  2977. else { <class of xx>.destroy is not valid }
  2978. InternalError(2014020601);
  2979. end
  2980. else
  2981. { Class style objects }
  2982. if is_class(methodpointer.resultdef) then
  2983. begin
  2984. { inherited call, no create/destroy }
  2985. if (cnf_inherited in callnodeflags) then
  2986. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2987. else
  2988. { do not create/destroy when called from member function
  2989. without specifying self explicit }
  2990. if (cnf_member_call in callnodeflags) then
  2991. begin
  2992. { destructor (in the same class, since cnf_member_call):
  2993. if not called from a destructor then
  2994. call beforedestruction and release instance, vmt=1
  2995. else
  2996. don't release instance, vmt=0
  2997. constructor (in the same class, since cnf_member_call):
  2998. if called from a constructor then
  2999. don't call afterconstruction, vmt=0
  3000. else
  3001. call afterconstrution but not NewInstance, vmt=-1 }
  3002. if (procdefinition.proctypeoption=potype_destructor) then
  3003. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  3004. vmttree:=cpointerconstnode.create(1,voidpointertype)
  3005. else
  3006. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3007. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  3008. (procdefinition.proctypeoption=potype_constructor) then
  3009. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3010. else
  3011. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  3012. end
  3013. else
  3014. { normal call to method like cl1.proc }
  3015. begin
  3016. { destructor:
  3017. if not(called from exception block in constructor) or
  3018. (called from afterconstruction)
  3019. call beforedestruction and release instance, vmt=1
  3020. else
  3021. don't call beforedestruction and release instance, vmt=-1
  3022. constructor:
  3023. if called from a constructor in the same class using self.create then
  3024. don't call afterconstruction, vmt=0
  3025. else
  3026. call afterconstruction, vmt=1 }
  3027. if (procdefinition.proctypeoption=potype_destructor) then
  3028. if (cnf_create_failed in callnodeflags) and
  3029. is_class(methodpointer.resultdef) then
  3030. vmttree:=call_vmt_node.getcopy
  3031. else if not(cnf_create_failed in callnodeflags) then
  3032. vmttree:=cpointerconstnode.create(1,voidpointertype)
  3033. else
  3034. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  3035. else
  3036. begin
  3037. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  3038. (procdefinition.proctypeoption=potype_constructor) and
  3039. (methodpointer.nodetype=loadn) and
  3040. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  3041. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3042. else
  3043. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  3044. end;
  3045. end;
  3046. end
  3047. else
  3048. { Old style object }
  3049. begin
  3050. { constructor with extended syntax called from new }
  3051. if (cnf_new_call in callnodeflags) then
  3052. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  3053. else
  3054. { destructor with extended syntax called from dispose }
  3055. { value -1 is what fpc_help_constructor() changes VMT to when it allocates memory }
  3056. if (cnf_dispose_call in callnodeflags) then
  3057. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  3058. else
  3059. { destructor called from exception block in constructor }
  3060. if (cnf_create_failed in callnodeflags) then
  3061. vmttree:=ctypeconvnode.create_internal(call_vmt_node.getcopy,voidpointertype)
  3062. else
  3063. { inherited call, no create/destroy }
  3064. if (cnf_inherited in callnodeflags) then
  3065. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3066. else
  3067. { do not create/destroy when called from member function
  3068. without specifying self explicit }
  3069. if (cnf_member_call in callnodeflags) then
  3070. begin
  3071. { destructor: don't release instance, vmt=0
  3072. constructor: don't initialize instance, vmt=0 }
  3073. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3074. end
  3075. else
  3076. { normal object call like obj.proc }
  3077. begin
  3078. { destructor: direct call, no dispose, vmt=0
  3079. constructor: initialize object, load vmt }
  3080. if (procdefinition.proctypeoption=potype_constructor) then
  3081. begin
  3082. { old styled inherited call? }
  3083. if (methodpointer.nodetype=typen) then
  3084. vmttree:=cpointerconstnode.create(0,voidpointertype)
  3085. else
  3086. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  3087. end
  3088. else
  3089. vmttree:=cpointerconstnode.create(0,voidpointertype);
  3090. end;
  3091. end;
  3092. result:=vmttree;
  3093. end;
  3094. function tcallnode.gen_block_context: tnode;
  3095. begin
  3096. { the self parameter of a block invocation is that address of the
  3097. block literal (which is what right contains) }
  3098. result:=right.getcopy;
  3099. end;
  3100. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  3101. var
  3102. destsym : tsym absolute arg;
  3103. begin
  3104. result := fen_false;
  3105. if (n.nodetype=loadn) and
  3106. (tloadnode(n).symtableentry = destsym) then
  3107. result := fen_norecurse_true;
  3108. end;
  3109. function check_funcret_temp_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  3110. var
  3111. tempinfo : ptempinfo absolute arg;
  3112. begin
  3113. result := fen_false;
  3114. if (n.nodetype=temprefn) and
  3115. (ttemprefnode(n).tempinfo = tempinfo) then
  3116. result := fen_norecurse_true;
  3117. end;
  3118. function tcallnode.funcret_can_be_reused:boolean;
  3119. var
  3120. realassignmenttarget: tnode;
  3121. alignment: longint;
  3122. begin
  3123. result:=false;
  3124. { we are processing an assignment node? }
  3125. if not(assigned(aktassignmentnode) and
  3126. (aktassignmentnode.right=self) and
  3127. (aktassignmentnode.left.resultdef=resultdef)) then
  3128. exit;
  3129. { destination must be able to be passed as var parameter }
  3130. if not valid_for_var(aktassignmentnode.left,false) then
  3131. exit;
  3132. { destination must be a simple load so it doesn't need a temp when
  3133. it is evaluated }
  3134. if not is_simple_para_load(aktassignmentnode.left,false) then
  3135. exit;
  3136. { remove possible typecasts }
  3137. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  3138. { when the result is returned by value (instead of by writing it to the
  3139. address passed in a hidden parameter), aktassignmentnode.left will
  3140. only be changed once the function has returned and we don't have to
  3141. perform any checks regarding whether it may alias with one of the
  3142. parameters -- unless this is an inline function, in which case
  3143. writes to the function result will directly change it and we do have
  3144. to check for potential aliasing }
  3145. if not paramanager.ret_in_param(resultdef,procdefinition) then
  3146. begin
  3147. if not(cnf_do_inline in callnodeflags) then
  3148. begin
  3149. result:=true;
  3150. exit;
  3151. end
  3152. else
  3153. begin
  3154. { don't replace the function result if we are inlining and if
  3155. the destination is complex, this could lead to lengthy
  3156. code in case the function result is used often and it is
  3157. assigned e.g. to a threadvar }
  3158. if node_complexity(aktassignmentnode.left)>1 then
  3159. exit;
  3160. end;
  3161. end;
  3162. { if the result is the same as the self parameter (in case of objects),
  3163. we can't optimise. We have to check this explicitly because
  3164. hidden parameters such as self have not yet been inserted at this
  3165. point
  3166. }
  3167. if assigned(methodpointer) and
  3168. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  3169. exit;
  3170. { when we substitute a function result inside an inlined function,
  3171. we may take the address of this function result. Therefore the
  3172. substituted function result may not be in a register, as we cannot
  3173. take its address in that case }
  3174. if (realassignmenttarget.nodetype=temprefn) and
  3175. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempflags) and
  3176. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempflags) then
  3177. begin
  3178. result:=not foreachnodestatic(left,@check_funcret_temp_used_as_para,ttemprefnode(realassignmenttarget).tempinfo);
  3179. exit;
  3180. end;
  3181. if (realassignmenttarget.nodetype=loadn) and
  3182. { nested procedures may access the current procedure's locals }
  3183. (procdefinition.parast.symtablelevel=normal_function_level) and
  3184. { must be a local variable, a value para or a hidden function result }
  3185. { parameter (which can be passed by address, but in that case it got }
  3186. { through these same checks at the caller side and is thus safe ) }
  3187. { other option: we're calling a compilerproc, because those don't
  3188. rely on global state
  3189. }
  3190. ((po_compilerproc in procdefinition.procoptions) or
  3191. (
  3192. (
  3193. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  3194. (
  3195. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  3196. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  3197. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  3198. )
  3199. ) and
  3200. { the address may not have been taken of the variable/parameter, because }
  3201. { otherwise it's possible that the called function can access it via a }
  3202. { global variable or other stored state }
  3203. (
  3204. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  3205. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  3206. )
  3207. )
  3208. ) then
  3209. begin
  3210. { If the funcret is also used as a parameter we can't optimize because the funcret
  3211. and the parameter will point to the same address. That means that a change of the result variable
  3212. will result also in a change of the parameter value }
  3213. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  3214. { ensure that it is aligned using the default alignment }
  3215. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  3216. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  3217. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  3218. result:=false;
  3219. exit;
  3220. end;
  3221. end;
  3222. procedure tcallnode.maybe_create_funcret_node;
  3223. var
  3224. temp : ttempcreatenode;
  3225. begin
  3226. if procdefinition.proctypeoption=potype_constructor then
  3227. exit;
  3228. { For the function result we need to create a temp node for:
  3229. - Inlined functions
  3230. - Types requiring initialization/finalization
  3231. - Types passed in parameters }
  3232. if not is_void(resultdef) and
  3233. not assigned(funcretnode) and
  3234. (
  3235. (cnf_do_inline in callnodeflags) or
  3236. is_managed_type(resultdef) or
  3237. paramanager.ret_in_param(resultdef,procdefinition)
  3238. ) then
  3239. begin
  3240. { Optimize calls like x:=f() where we can use x directly as
  3241. result instead of using a temp. Condition is that x cannot be accessed from f().
  3242. This implies that x is a local variable or value parameter of the current block
  3243. and its address is not passed to f. One problem: what if someone takes the
  3244. address of x, puts it in a pointer variable/field and then accesses it that way
  3245. from within the function? This is solved (in a conservative way) using the
  3246. ti_addr_taken flag.
  3247. When the result is not not passed in a parameter there are no problem because
  3248. then it means only reference counted types (eg. ansistrings) that need a decr
  3249. of the refcount before being assigned. This is all done after the call so there
  3250. is no issue with exceptions and possible use of the old value in the called
  3251. function }
  3252. if funcret_can_be_reused then
  3253. begin
  3254. funcretnode:=aktassignmentnode.left.getcopy;
  3255. include(funcretnode.flags,nf_is_funcret);
  3256. { notify the assignment node that the assignment can be removed }
  3257. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3258. end
  3259. else
  3260. begin
  3261. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  3262. (cnf_do_inline in callnodeflags) and
  3263. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  3264. include(temp.flags,nf_is_funcret);
  3265. { if a managed type is returned by reference, assigning something
  3266. to the result on the caller side will take care of decreasing
  3267. the reference count }
  3268. if paramanager.ret_in_param(resultdef,procdefinition) then
  3269. temp.includetempflag(ti_nofini);
  3270. add_init_statement(temp);
  3271. { When the function result is not used in an inlined function
  3272. we need to delete the temp. This can currently only be done by
  3273. a tempdeletenode and not after converting it to a normal temp }
  3274. if not(cnf_return_value_used in callnodeflags) and
  3275. (cnf_do_inline in callnodeflags) then
  3276. add_done_statement(ctempdeletenode.create(temp))
  3277. else
  3278. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  3279. funcretnode:=ctemprefnode.create(temp);
  3280. include(funcretnode.flags,nf_is_funcret);
  3281. end;
  3282. end;
  3283. end;
  3284. procedure tcallnode.gen_hidden_parameters;
  3285. var
  3286. para : tcallparanode;
  3287. begin
  3288. para:=tcallparanode(left);
  3289. while assigned(para) do
  3290. begin
  3291. { The processing of high() and typeinfo() is already
  3292. done in the typecheckpass. We only need to process the
  3293. nodes that still have a nothingn }
  3294. if (vo_is_hidden_para in para.parasym.varoptions) and
  3295. (para.left.nodetype=nothingn) then
  3296. begin
  3297. { remove dummy nothingn }
  3298. para.left.free;
  3299. para.left:=nil;
  3300. { generate the corresponding nodes for the hidden parameter type }
  3301. if (vo_is_funcret in para.parasym.varoptions) then
  3302. begin
  3303. if not assigned(funcretnode) then
  3304. internalerror(200709083);
  3305. { if funcretnode is a temprefnode, we have to keep it intact
  3306. if it may have been created in maybe_create_funcret_node(),
  3307. because then it will also be destroyed by a
  3308. ctempdeletenode.create_normal_temp() in the cleanup code
  3309. for this call code. In that case we have to copy this
  3310. ttemprefnode after the tempdeletenode to reset its
  3311. tempinfo^.hookoncopy. This is done by copying funcretnode
  3312. in tcallnode.getcopy(), but for that to work we can't reset
  3313. funcretnode to nil here. }
  3314. if (funcretnode.nodetype<>temprefn) or
  3315. (not(cnf_return_value_used in callnodeflags) and
  3316. (cnf_do_inline in callnodeflags)) then
  3317. begin
  3318. para.left:=funcretnode;
  3319. funcretnode:=nil;
  3320. end
  3321. else
  3322. para.left:=funcretnode.getcopy;
  3323. end
  3324. else
  3325. if vo_is_self in para.parasym.varoptions then
  3326. begin
  3327. if assigned(right) then
  3328. para.left:=gen_procvar_context_tree_self
  3329. else
  3330. para.left:=gen_self_tree;
  3331. { make sure that e.g. the self pointer of an advanced
  3332. record does not become a regvar, because it's a vs_var
  3333. parameter }
  3334. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  3335. procdefinition.proccalloption) then
  3336. make_not_regable(para.left,[ra_addr_regable]);
  3337. end
  3338. else
  3339. if vo_is_vmt in para.parasym.varoptions then
  3340. begin
  3341. para.left:=gen_vmt_tree;
  3342. end
  3343. else
  3344. if vo_is_syscall_lib in para.parasym.varoptions then
  3345. gen_syscall_para(para)
  3346. else
  3347. if vo_is_range_check in para.parasym.varoptions then
  3348. begin
  3349. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool1type,false);
  3350. end
  3351. else
  3352. if vo_is_overflow_check in para.parasym.varoptions then
  3353. begin
  3354. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool1type,false);
  3355. end
  3356. else
  3357. if vo_is_msgsel in para.parasym.varoptions then
  3358. begin
  3359. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  3360. end;
  3361. end;
  3362. if not assigned(para.left) then
  3363. internalerror(200709084);
  3364. para:=tcallparanode(para.right);
  3365. end;
  3366. end;
  3367. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  3368. var
  3369. pd : tprocdef;
  3370. i : longint;
  3371. j : integer;
  3372. hs : string;
  3373. begin
  3374. if (tsym(sym).typ<>procsym) then
  3375. exit;
  3376. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  3377. begin
  3378. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  3379. hs:=pd.procsym.name+pd.typename_paras([]);
  3380. j:=AbstractMethodsList.FindIndexOf(hs);
  3381. if j<>-1 then
  3382. AbstractMethodsList[j]:=pd
  3383. else
  3384. AbstractMethodsList.Add(hs,pd);
  3385. end;
  3386. end;
  3387. procedure tcallnode.verifyabstractcalls;
  3388. var
  3389. objectdf : tobjectdef;
  3390. parents : tlinkedlist;
  3391. objectinfo : tobjectinfoitem;
  3392. pd : tprocdef;
  3393. i : integer;
  3394. begin
  3395. objectdf := nil;
  3396. { verify if trying to create an instance of a class which contains
  3397. non-implemented abstract methods }
  3398. { first verify this class type, no class than exit }
  3399. { also, this checking can only be done if the constructor is directly
  3400. called, indirect constructor calls cannot be checked.
  3401. }
  3402. if assigned(methodpointer) and
  3403. not((methodpointer.nodetype=loadn) and
  3404. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  3405. begin
  3406. if (methodpointer.resultdef.typ = objectdef) then
  3407. objectdf:=tobjectdef(methodpointer.resultdef)
  3408. else
  3409. if (methodpointer.resultdef.typ = classrefdef) and
  3410. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  3411. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  3412. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  3413. end;
  3414. if not assigned(objectdf) then
  3415. exit;
  3416. { quick exit if nothing to check }
  3417. if objectdf.abstractcnt = 0 then
  3418. exit;
  3419. parents := tlinkedlist.create;
  3420. AbstractMethodsList := TFPHashList.create;
  3421. { insert all parents in this class : the first item in the
  3422. list will be the base parent of the class .
  3423. }
  3424. while assigned(objectdf) do
  3425. begin
  3426. objectinfo:=tobjectinfoitem.create(objectdf);
  3427. parents.insert(objectinfo);
  3428. objectdf := objectdf.childof;
  3429. end;
  3430. { now all parents are in the correct order
  3431. insert all abstract methods in the list, and remove
  3432. those which are overridden by parent classes.
  3433. }
  3434. objectinfo:=tobjectinfoitem(parents.first);
  3435. while assigned(objectinfo) do
  3436. begin
  3437. objectdf := objectinfo.objinfo;
  3438. if assigned(objectdf.symtable) then
  3439. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  3440. objectinfo:=tobjectinfoitem(objectinfo.next);
  3441. end;
  3442. if assigned(parents) then
  3443. parents.free;
  3444. { Finally give out a warning for each abstract method still in the list }
  3445. for i:=0 to AbstractMethodsList.Count-1 do
  3446. begin
  3447. pd:=tprocdef(AbstractMethodsList[i]);
  3448. if po_abstractmethod in pd.procoptions then
  3449. begin
  3450. Message2(type_w_instance_with_abstract,objectdf.typesymbolprettyname,pd.customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker, pno_prettynames]));
  3451. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  3452. end;
  3453. end;
  3454. if assigned(AbstractMethodsList) then
  3455. AbstractMethodsList.Free;
  3456. end;
  3457. procedure tcallnode.convert_carg_array_of_const;
  3458. var
  3459. hp : tarrayconstructornode;
  3460. oldleft : tcallparanode;
  3461. begin
  3462. oldleft:=tcallparanode(left);
  3463. if oldleft.left.nodetype<>arrayconstructorn then
  3464. begin
  3465. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  3466. exit;
  3467. end;
  3468. include(callnodeflags,cnf_uses_varargs);
  3469. { Get arrayconstructor node and insert typeconvs }
  3470. hp:=tarrayconstructornode(oldleft.left);
  3471. { Add c args parameters }
  3472. { It could be an empty set }
  3473. if assigned(hp) and
  3474. assigned(hp.left) then
  3475. begin
  3476. while assigned(hp) do
  3477. begin
  3478. left:=ccallparanode.create(hp.left,left);
  3479. { set callparanode resultdef and flags }
  3480. left.resultdef:=hp.left.resultdef;
  3481. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  3482. hp.left:=nil;
  3483. hp:=tarrayconstructornode(hp.right);
  3484. end;
  3485. end;
  3486. { Remove value of old array of const parameter, but keep it
  3487. in the list because it is required for bind_parasym.
  3488. Generate a nothign to keep callparanoed.left valid }
  3489. oldleft.left.free;
  3490. oldleft.left:=cnothingnode.create;
  3491. end;
  3492. procedure tcallnode.bind_parasym;
  3493. type
  3494. pcallparanode = ^tcallparanode;
  3495. var
  3496. i : integer;
  3497. pt : tcallparanode;
  3498. oldppt : pcallparanode;
  3499. varargspara,
  3500. currpara : tparavarsym;
  3501. hiddentree : tnode;
  3502. paradef : tdef;
  3503. begin
  3504. pt:=tcallparanode(left);
  3505. oldppt:=pcallparanode(@left);
  3506. { flag all callparanodes that belong to the varargs }
  3507. i:=paralength;
  3508. while (i>procdefinition.maxparacount) do
  3509. begin
  3510. include(pt.callparaflags,cpf_varargs_para);
  3511. oldppt:=pcallparanode(@pt.right);
  3512. pt:=tcallparanode(pt.right);
  3513. dec(i);
  3514. end;
  3515. { skip varargs that are inserted by array of const }
  3516. while assigned(pt) and
  3517. (cpf_varargs_para in pt.callparaflags) do
  3518. pt:=tcallparanode(pt.right);
  3519. { process normal parameters and insert hidden parameter nodes, the content
  3520. of the hidden parameters will be updated in pass1 }
  3521. for i:=procdefinition.paras.count-1 downto 0 do
  3522. begin
  3523. currpara:=tparavarsym(procdefinition.paras[i]);
  3524. if vo_is_hidden_para in currpara.varoptions then
  3525. begin
  3526. { Here we handle only the parameters that depend on
  3527. the types of the previous parameter. The typeconversion
  3528. can change the type in the next step. For example passing
  3529. an array can be change to a pointer and a deref.
  3530. We also handle the generation of parentfp parameters, as they
  3531. must all be created before pass_1 on targets that use explicit
  3532. parentfp structs (rather than the frame pointer). The reason
  3533. is that the necessary initialisation code for the these
  3534. structures is attached to the procedure's nodetree after
  3535. the resulttype pass.
  3536. }
  3537. if vo_is_high_para in currpara.varoptions then
  3538. begin
  3539. if not assigned(pt) or (i=0) then
  3540. internalerror(200304081);
  3541. { we need the information of the previous parameter }
  3542. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  3543. hiddentree:=gen_high_tree(pt.left,paradef);
  3544. { for open array of managed type, a copy of high parameter is
  3545. necessary to properly initialize before the call }
  3546. if is_open_array(paradef) and
  3547. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  3548. is_managed_type(tarraydef(paradef).elementdef) then
  3549. begin
  3550. typecheckpass(hiddentree);
  3551. {this eliminates double call to fpc_dynarray_high, if any}
  3552. maybe_load_in_temp(hiddentree);
  3553. oldppt^.third:=hiddentree.getcopy;
  3554. end;
  3555. end
  3556. else
  3557. if vo_is_typinfo_para in currpara.varoptions then
  3558. begin
  3559. if not assigned(pt) or (i=0) then
  3560. internalerror(200304082);
  3561. hiddentree:=caddrnode.create_internal(
  3562. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  3563. );
  3564. end
  3565. else if vo_is_parentfp in currpara.varoptions then
  3566. begin
  3567. if assigned(right) and (right.resultdef.typ=procvardef) and
  3568. not tabstractprocdef(right.resultdef).is_addressonly then
  3569. maybe_load_in_temp(right);
  3570. if not assigned(right) then
  3571. begin
  3572. if assigned(procdefinition.owner.defowner) then
  3573. begin
  3574. if paramanager.can_opt_unused_para(currpara) then
  3575. { If parentfp is unused by the target proc, create a dummy
  3576. pointerconstnode which will be discarded later. }
  3577. hiddentree:=cpointerconstnode.create(0,currpara.vardef)
  3578. else
  3579. begin
  3580. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara);
  3581. if is_nested_pd(current_procinfo.procdef) then
  3582. current_procinfo.set_needs_parentfp(tprocdef(procdefinition.owner.defowner).parast.symtablelevel);
  3583. end;
  3584. end
  3585. { exceptfilters called from main level are not owned }
  3586. else if procdefinition.proctypeoption=potype_exceptfilter then
  3587. hiddentree:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  3588. else
  3589. internalerror(200309287);
  3590. end
  3591. else if not(po_is_block in procdefinition.procoptions) then
  3592. hiddentree:=gen_procvar_context_tree_parentfp
  3593. else
  3594. hiddentree:=gen_block_context
  3595. end
  3596. else
  3597. hiddentree:=cnothingnode.create;
  3598. pt:=ccallparanode.create(hiddentree,oldppt^);
  3599. oldppt^:=pt;
  3600. end;
  3601. if not assigned(pt) then
  3602. internalerror(200310052);
  3603. pt.parasym:=currpara;
  3604. oldppt:=pcallparanode(@pt.right);
  3605. pt:=tcallparanode(pt.right);
  3606. end;
  3607. { Create parasyms for varargs, first count the number of varargs paras,
  3608. then insert the parameters with numbering in reverse order. The SortParas
  3609. will set the correct order at the end}
  3610. pt:=tcallparanode(left);
  3611. i:=0;
  3612. while assigned(pt) do
  3613. begin
  3614. if cpf_varargs_para in pt.callparaflags then
  3615. inc(i);
  3616. pt:=tcallparanode(pt.right);
  3617. end;
  3618. if (i>0) then
  3619. begin
  3620. include(current_procinfo.flags,pi_calls_c_varargs);
  3621. varargsparas:=tvarargsparalist.create;
  3622. pt:=tcallparanode(left);
  3623. while assigned(pt) do
  3624. begin
  3625. if cpf_varargs_para in pt.callparaflags then
  3626. begin
  3627. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  3628. dec(i);
  3629. { varargspara is left-right, use insert
  3630. instead of concat }
  3631. varargsparas.add(varargspara);
  3632. pt.parasym:=varargspara;
  3633. end;
  3634. pt:=tcallparanode(pt.right);
  3635. end;
  3636. varargsparas.sortparas;
  3637. end;
  3638. end;
  3639. function tcallnode.pass_typecheck:tnode;
  3640. function is_undefined_recursive(def:tdef):boolean;
  3641. begin
  3642. { might become more refined in the future }
  3643. if def.typ=undefineddef then
  3644. result:=true
  3645. else if def.typ=arraydef then
  3646. result:=is_undefined_recursive(tarraydef(def).elementdef)
  3647. else
  3648. result:=false;
  3649. end;
  3650. var
  3651. candidates : tcallcandidates;
  3652. ccflags : tcallcandidatesflags;
  3653. oldcallnode : tcallnode;
  3654. hpt,tmp : tnode;
  3655. pt : tcallparanode;
  3656. lastpara : longint;
  3657. paraidx,
  3658. cand_cnt : integer;
  3659. i : longint;
  3660. ignoregenericparacall,
  3661. is_const : boolean;
  3662. statements : tstatementnode;
  3663. converted_result_data : ttempcreatenode;
  3664. calltype: tdispcalltype;
  3665. invokesym : tsym;
  3666. begin
  3667. result:=nil;
  3668. oldcallnode:=aktcallnode;
  3669. aktcallnode:=self;
  3670. try
  3671. { determine length of parameter list }
  3672. pt:=tcallparanode(left);
  3673. paralength:=0;
  3674. while assigned(pt) do
  3675. begin
  3676. inc(paralength);
  3677. pt:=tcallparanode(pt.right);
  3678. end;
  3679. { determine the type of the parameters }
  3680. if assigned(left) then
  3681. begin
  3682. tcallparanode(left).get_paratype;
  3683. if codegenerror then
  3684. exit;
  3685. end;
  3686. if assigned(methodpointer) then
  3687. typecheckpass(methodpointer);
  3688. { procedure variable ? }
  3689. if assigned(right) then
  3690. begin
  3691. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3692. typecheckpass(right);
  3693. if codegenerror then
  3694. exit;
  3695. if is_invokable(right.resultdef) then
  3696. begin
  3697. procdefinition:=get_invoke_procdef(tobjectdef(right.resultdef));
  3698. if assigned(methodpointer) then
  3699. internalerror(2021041004);
  3700. methodpointer:=right;
  3701. { don't convert again when this is used as the self parameter }
  3702. include(right.flags,nf_load_procvar);
  3703. right:=nil;
  3704. end
  3705. else
  3706. procdefinition:=tabstractprocdef(right.resultdef);
  3707. { Compare parameters from right to left }
  3708. paraidx:=procdefinition.Paras.count-1;
  3709. { Skip default parameters }
  3710. if not(po_varargs in procdefinition.procoptions) then
  3711. begin
  3712. { ignore hidden parameters }
  3713. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3714. dec(paraidx);
  3715. for i:=1 to procdefinition.maxparacount-paralength do
  3716. begin
  3717. if paraidx<0 then
  3718. internalerror(200402265);
  3719. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3720. begin
  3721. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3722. exit;
  3723. end;
  3724. dec(paraidx);
  3725. end;
  3726. end;
  3727. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3728. dec(paraidx);
  3729. pt:=tcallparanode(left);
  3730. lastpara:=paralength;
  3731. while (paraidx>=0) and assigned(pt) do
  3732. begin
  3733. { only goto next para if we're out of the varargs }
  3734. if not(po_varargs in procdefinition.procoptions) or
  3735. (lastpara<=procdefinition.maxparacount) then
  3736. begin
  3737. repeat
  3738. dec(paraidx);
  3739. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3740. end;
  3741. pt:=tcallparanode(pt.right);
  3742. dec(lastpara);
  3743. end;
  3744. if assigned(pt) or
  3745. ((paraidx>=0) and
  3746. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  3747. begin
  3748. if assigned(pt) then
  3749. current_filepos:=pt.fileinfo;
  3750. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3751. exit;
  3752. end;
  3753. end
  3754. else
  3755. { not a procedure variable }
  3756. begin
  3757. { do we know the procedure to call ? }
  3758. if not(assigned(procdefinition)) then
  3759. begin
  3760. { according to bug reports 32539 and 20551, real variant of sqr/abs should be used when they are called for variants to be
  3761. delphi compatible, this is in contrast to normal overloading behaviour, so fix this by a terrible hack to be compatible }
  3762. if assigned(left) and assigned(tcallparanode(left).left) and
  3763. (tcallparanode(left).left.resultdef.typ=variantdef) and assigned(symtableproc.name) and (symtableproc.name^='SYSTEM') then
  3764. begin
  3765. if symtableprocentry.Name='SQR' then
  3766. begin
  3767. result:=cinlinenode.createintern(in_sqr_real,false,tcallparanode(left).left.getcopy);
  3768. exit;
  3769. end;
  3770. if symtableprocentry.Name='ABS' then
  3771. begin
  3772. result:=cinlinenode.createintern(in_abs_real,false,tcallparanode(left).left.getcopy);
  3773. exit;
  3774. end;
  3775. end;
  3776. ccflags:=[];
  3777. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3778. if (nf_isproperty in flags) or
  3779. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags)) or
  3780. (cnf_ignore_visibility in callnodeflags)
  3781. then
  3782. ccflags:=ccflags+[cc_ignorevisibility];
  3783. if not(nf_isproperty in flags) then
  3784. ccflags:=ccflags+[cc_allowdefaultparas];
  3785. if cnf_objc_id_call in callnodeflags then
  3786. ccflags:=ccflags+[cc_objcidcall];
  3787. if cnf_unit_specified in callnodeflags then
  3788. ccflags:=ccflags+[cc_explicitunit];
  3789. if callnodeflags*[cnf_anon_inherited,cnf_inherited]=[] then
  3790. ccflags:=ccflags+[cc_searchhelpers];
  3791. if cnf_anon_inherited in callnodeflags then
  3792. ccflags:=ccflags+[cc_anoninherited];
  3793. candidates.init(symtableprocentry,symtableproc,left,ccflags,spezcontext);
  3794. { no procedures found? then there is something wrong
  3795. with the parameter size or the procedures are
  3796. not accessible }
  3797. if candidates.count=0 then
  3798. begin
  3799. { when it's an auto inherited call and there
  3800. is no procedure found, but the procedures
  3801. were defined with overload directive and at
  3802. least two procedures are defined then we ignore
  3803. this inherited by inserting a nothingn. Only
  3804. do this ugly hack in Delphi mode as it looks more
  3805. like a bug. It's also not documented }
  3806. if (m_delphi in current_settings.modeswitches) and
  3807. (cnf_anon_inherited in callnodeflags) and
  3808. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  3809. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  3810. (symtableprocentry.ProcdefList.Count>=2) then
  3811. result:=cnothingnode.create
  3812. else
  3813. begin
  3814. { in tp mode we can try to convert to procvar if
  3815. there are no parameters specified }
  3816. if not(assigned(left)) and
  3817. ([cnf_inherited,cnf_no_convert_procvar]*callnodeflags=[]) and
  3818. ((m_tp_procvar in current_settings.modeswitches) or
  3819. (m_mac_procvar in current_settings.modeswitches)) and
  3820. (not assigned(methodpointer) or
  3821. (methodpointer.nodetype <> typen)) then
  3822. begin
  3823. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  3824. if assigned(methodpointer) then
  3825. tloadnode(hpt).set_mp(methodpointer.getcopy);
  3826. typecheckpass(hpt);
  3827. result:=hpt;
  3828. end
  3829. else
  3830. begin
  3831. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  3832. symtableprocentry.write_parameter_lists(nil);
  3833. end;
  3834. end;
  3835. candidates.done;
  3836. exit;
  3837. end;
  3838. { Retrieve information about the candidates }
  3839. candidates.get_information;
  3840. {$ifdef EXTDEBUG}
  3841. { Display info when multiple candidates are found }
  3842. if candidates.count>1 then
  3843. candidates.dump_info(V_Debug);
  3844. {$endif EXTDEBUG}
  3845. { Choose the best candidate and count the number of
  3846. candidates left }
  3847. cand_cnt:=candidates.choose_best(procdefinition,
  3848. assigned(left) and
  3849. not assigned(tcallparanode(left).right) and
  3850. (tcallparanode(left).left.resultdef.typ=variantdef));
  3851. { All parameters are checked, check if there are any
  3852. procedures left }
  3853. if cand_cnt>0 then
  3854. begin
  3855. { Multiple candidates left? }
  3856. if cand_cnt>1 then
  3857. begin
  3858. { if we're inside a generic and call another function
  3859. with generic types as arguments we don't complain in
  3860. the generic, but only during the specialization }
  3861. ignoregenericparacall:=false;
  3862. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  3863. begin
  3864. pt:=tcallparanode(left);
  3865. while assigned(pt) do
  3866. begin
  3867. if is_undefined_recursive(pt.resultdef) then
  3868. begin
  3869. ignoregenericparacall:=true;
  3870. break;
  3871. end;
  3872. pt:=tcallparanode(pt.right);
  3873. end;
  3874. end;
  3875. if not ignoregenericparacall then
  3876. begin
  3877. CGMessage(type_e_cant_choose_overload_function);
  3878. {$ifdef EXTDEBUG}
  3879. candidates.dump_info(V_Hint);
  3880. {$else EXTDEBUG}
  3881. candidates.list(false);
  3882. {$endif EXTDEBUG}
  3883. end;
  3884. { we'll just use the first candidate to make the
  3885. call }
  3886. end;
  3887. { assign procdefinition }
  3888. if symtableproc=nil then
  3889. symtableproc:=procdefinition.owner;
  3890. end
  3891. else
  3892. begin
  3893. { No candidates left, this must be a type error,
  3894. because wrong size is already checked. procdefinition
  3895. is filled with the first (random) definition that is
  3896. found. We use this definition to display a nice error
  3897. message that the wrong type is passed }
  3898. candidates.find_wrong_para;
  3899. candidates.list(true);
  3900. {$ifdef EXTDEBUG}
  3901. candidates.dump_info(V_Hint);
  3902. {$endif EXTDEBUG}
  3903. { We can not proceed, release all procs and exit }
  3904. candidates.done;
  3905. exit;
  3906. end;
  3907. { if the final procedure definition is not yet owned,
  3908. ensure that it is }
  3909. procdefinition.register_def;
  3910. if procdefinition.is_specialization and (procdefinition.typ=procdef) then
  3911. maybe_add_pending_specialization(procdefinition,candidates.para_anon_syms);
  3912. candidates.done;
  3913. end; { end of procedure to call determination }
  3914. end;
  3915. if procdefinition.typ = procdef then
  3916. begin
  3917. { check for hints (deprecated etc) }
  3918. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions,tprocdef(procdefinition).deprecatedmsg);
  3919. { add reference to corresponding procsym; may not be the one
  3920. originally found/passed to the constructor because of overloads }
  3921. addsymref(tprocdef(procdefinition).procsym,procdefinition);
  3922. { ensure that the generic is considered as used as for an
  3923. implicit specialization must only be called after the final
  3924. overload was picked }
  3925. if assigned(tprocdef(procdefinition).genericdef) and
  3926. assigned(tprocdef(tprocdef(procdefinition).genericdef).procsym) and
  3927. (tprocdef(tprocdef(procdefinition).genericdef).procsym.refs=0) then
  3928. addsymref(tprocdef(tprocdef(procdefinition).genericdef).procsym);
  3929. end;
  3930. { add needed default parameters }
  3931. if (paralength<procdefinition.maxparacount) then
  3932. begin
  3933. paraidx:=0;
  3934. i:=0;
  3935. while (i<paralength) do
  3936. begin
  3937. if paraidx>=procdefinition.Paras.count then
  3938. internalerror(200306181);
  3939. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  3940. inc(i);
  3941. inc(paraidx);
  3942. end;
  3943. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3944. inc(paraidx);
  3945. while (paraidx<procdefinition.paras.count) do
  3946. begin
  3947. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3948. internalerror(200212142);
  3949. left:=ccallparanode.create(genconstsymtree(
  3950. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  3951. { Ignore vs_hidden parameters }
  3952. repeat
  3953. inc(paraidx);
  3954. until (paraidx>=procdefinition.paras.count) or
  3955. not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3956. end;
  3957. end;
  3958. { recursive call? }
  3959. if assigned(current_procinfo) and
  3960. (procdefinition=current_procinfo.procdef) then
  3961. include(current_procinfo.flags,pi_is_recursive);
  3962. { handle predefined procedures }
  3963. is_const:=(po_internconst in procdefinition.procoptions) and
  3964. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  3965. (assigned(left) and ((tcallparanode(left).left.nodetype in [realconstn,ordconstn])
  3966. and (not assigned(tcallparanode(left).right) or (tcallparanode(tcallparanode(left).right).left.nodetype in [realconstn,ordconstn])))));
  3967. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  3968. begin
  3969. if assigned(left) then
  3970. begin
  3971. { convert types to those of the prototype, this is required by functions like ror, rol, sar
  3972. some use however a dummy type (Typedfile) so this would break them }
  3973. if not(tinlinenumber(tprocdef(procdefinition).extnumber) in
  3974. [in_Reset_TypedFile,in_Rewrite_TypedFile,in_reset_typedfile_name,in_rewrite_typedfile_name]) then
  3975. begin
  3976. { bind parasyms to the callparanodes and insert hidden parameters }
  3977. bind_parasym;
  3978. { insert type conversions for parameters }
  3979. if assigned(left) then
  3980. tcallparanode(left).insert_typeconv;
  3981. end;
  3982. { ptr and settextbuf need two args }
  3983. if assigned(tcallparanode(left).right) then
  3984. begin
  3985. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,left);
  3986. left:=nil;
  3987. end
  3988. else
  3989. begin
  3990. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,tcallparanode(left).left);
  3991. tcallparanode(left).left:=nil;
  3992. end;
  3993. end
  3994. else
  3995. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,nil);
  3996. result:=hpt;
  3997. exit;
  3998. end;
  3999. { in case this is an Objective-C message that returns a related object type by convention,
  4000. override the default result type }
  4001. if po_objc_related_result_type in procdefinition.procoptions then
  4002. begin
  4003. { don't crash in case of syntax errors }
  4004. if assigned(methodpointer) then
  4005. begin
  4006. include(callnodeflags,cnf_typedefset);
  4007. typedef:=methodpointer.resultdef;
  4008. if typedef.typ=classrefdef then
  4009. typedef:=tclassrefdef(typedef).pointeddef;
  4010. end;
  4011. end;
  4012. { ensure that the result type is set }
  4013. if not(cnf_typedefset in callnodeflags) then
  4014. begin
  4015. { constructors return their current class type, not the type where the
  4016. constructor is declared, this can be different because of inheritance }
  4017. if (procdefinition.proctypeoption=potype_constructor) and
  4018. assigned(methodpointer) and
  4019. assigned(methodpointer.resultdef) and
  4020. (methodpointer.resultdef.typ=classrefdef) then
  4021. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  4022. else
  4023. { Member call to a (inherited) constructor from the class, the return
  4024. value is always self, so we change it to voidtype to generate an
  4025. error and to prevent users from generating non-working code
  4026. when they expect to clone the current instance, see bug 3662 (PFV) }
  4027. if (procdefinition.proctypeoption=potype_constructor) and
  4028. is_class(tprocdef(procdefinition).struct) and
  4029. assigned(methodpointer) and
  4030. (methodpointer.nodetype=loadn) and
  4031. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  4032. resultdef:=voidtype
  4033. else
  4034. resultdef:=procdefinition.returndef;
  4035. end
  4036. else
  4037. resultdef:=typedef;
  4038. { Check object/class for methods }
  4039. if assigned(methodpointer) then
  4040. begin
  4041. { direct call to inherited abstract method, then we
  4042. can already give a error in the compiler instead
  4043. of a runtime error }
  4044. if (cnf_inherited in callnodeflags) and
  4045. (po_abstractmethod in procdefinition.procoptions) then
  4046. begin
  4047. if (m_delphi in current_settings.modeswitches) and
  4048. (cnf_anon_inherited in callnodeflags) then
  4049. begin
  4050. CGMessage(cg_h_inherited_ignored);
  4051. result:=cnothingnode.create;
  4052. exit;
  4053. end
  4054. else
  4055. CGMessage(cg_e_cant_call_abstract_method);
  4056. end;
  4057. { directly calling an interface/protocol/category/class helper
  4058. method via its type is not possible (always must be called via
  4059. the actual instance) }
  4060. if (methodpointer.nodetype=typen) and
  4061. ((
  4062. is_interface(methodpointer.resultdef) and not
  4063. is_objectpascal_helper(tdef(procdefinition.owner.defowner))
  4064. ) or
  4065. is_objc_protocol_or_category(methodpointer.resultdef)) then
  4066. CGMessage1(type_e_class_type_expected,methodpointer.resultdef.typename);
  4067. { if an inherited con- or destructor should be }
  4068. { called in a con- or destructor then a warning }
  4069. { will be made }
  4070. { con- and destructors need a pointer to the vmt }
  4071. if (cnf_inherited in callnodeflags) and
  4072. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  4073. is_object(methodpointer.resultdef) and
  4074. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  4075. CGMessage(cg_w_member_cd_call_from_method);
  4076. if methodpointer.nodetype<>typen then
  4077. begin
  4078. hpt:=methodpointer;
  4079. { Remove all postfix operators }
  4080. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  4081. hpt:=tunarynode(hpt).left;
  4082. if ((hpt.nodetype=loadvmtaddrn) or
  4083. ((hpt.nodetype=loadn) and assigned(tloadnode(hpt).resultdef) and (tloadnode(hpt).resultdef.typ=classrefdef))) and
  4084. not (procdefinition.proctypeoption=potype_constructor) and
  4085. not (po_classmethod in procdefinition.procoptions) and
  4086. not (po_staticmethod in procdefinition.procoptions) then
  4087. { error: we are calling instance method from the class method/static method }
  4088. CGMessage(parser_e_only_class_members);
  4089. if (procdefinition.proctypeoption=potype_constructor) and
  4090. assigned(symtableproc) and
  4091. (symtableproc.symtabletype=withsymtable) and
  4092. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  4093. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  4094. { skip (absolute and other simple) type conversions -- only now,
  4095. because the checks above have to take type conversions into
  4096. e.g. class reference types account }
  4097. hpt:=actualtargetnode(@hpt)^;
  4098. { R.Init then R will be initialized by the constructor,
  4099. Also allow it for simple loads }
  4100. if (procdefinition.proctypeoption=potype_constructor) or
  4101. ((hpt.nodetype=loadn) and
  4102. (((methodpointer.resultdef.typ=objectdef) and
  4103. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  4104. (methodpointer.resultdef.typ=recorddef)
  4105. )
  4106. ) then
  4107. { a constructor will and a method may write something to }
  4108. { the fields }
  4109. set_varstate(methodpointer,vs_readwritten,[])
  4110. else
  4111. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  4112. end;
  4113. { if we are calling the constructor check for abstract
  4114. methods. Ignore inherited and member calls, because the
  4115. class is then already created }
  4116. if (procdefinition.proctypeoption=potype_constructor) and
  4117. not(cnf_inherited in callnodeflags) and
  4118. not(cnf_member_call in callnodeflags) then
  4119. verifyabstractcalls;
  4120. end
  4121. else
  4122. begin
  4123. { When this is method the methodpointer must be available }
  4124. if (right=nil) and
  4125. (procdefinition.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  4126. not procdefinition.no_self_node then
  4127. internalerror(200305061);
  4128. end;
  4129. { Set flag that the procedure uses varargs, also if they are not passed it is still
  4130. needed for x86_64 to pass the number of SSE registers used }
  4131. if po_varargs in procdefinition.procoptions then
  4132. include(callnodeflags,cnf_uses_varargs);
  4133. { set the appropriate node flag if the call never returns }
  4134. if po_noreturn in procdefinition.procoptions then
  4135. include(callnodeflags,cnf_call_never_returns);
  4136. { Change loading of array of const to varargs }
  4137. if assigned(left) and
  4138. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  4139. (procdefinition.proccalloption in cdecl_pocalls) then
  4140. convert_carg_array_of_const;
  4141. { bind parasyms to the callparanodes and insert hidden parameters }
  4142. bind_parasym;
  4143. { insert type conversions for parameters }
  4144. if assigned(left) then
  4145. tcallparanode(left).insert_typeconv;
  4146. { dispinterface methode invoke? }
  4147. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  4148. begin
  4149. case procdefinition.proctypeoption of
  4150. potype_propgetter: calltype:=dct_propget;
  4151. potype_propsetter: calltype:=dct_propput;
  4152. else
  4153. calltype:=dct_method;
  4154. end;
  4155. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  4156. if (cnf_return_value_used in callnodeflags) and not is_void(procdefinition.returndef) then
  4157. begin
  4158. result:=internalstatements(statements);
  4159. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),
  4160. tt_persistent,true);
  4161. addstatement(statements,converted_result_data);
  4162. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  4163. ctypeconvnode.create_internal(
  4164. translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,procdefinition.returndef),
  4165. procdefinition.returndef)));
  4166. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  4167. addstatement(statements,ctemprefnode.create(converted_result_data));
  4168. end
  4169. else
  4170. result:=translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,voidtype);
  4171. { don't free reused nodes }
  4172. methodpointer:=nil;
  4173. parameters:=nil;
  4174. end;
  4175. maybe_gen_call_self_node;
  4176. if assigned(call_self_node) then
  4177. typecheckpass(call_self_node);
  4178. if assigned(call_vmt_node) then
  4179. typecheckpass(call_vmt_node);
  4180. if assigned(current_procinfo) and
  4181. (procdefinition.typ=procdef) and
  4182. (procdefinition.parast.symtablelevel<=current_procinfo.procdef.parast.symtablelevel) and
  4183. (procdefinition.parast.symtablelevel>normal_function_level) and
  4184. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  4185. current_procinfo.add_captured_sym(tprocdef(procdefinition).procsym,procdefinition,fileinfo);
  4186. finally
  4187. aktcallnode:=oldcallnode;
  4188. end;
  4189. end;
  4190. function tcallnode.simplify(forinline : boolean) : tnode;
  4191. begin
  4192. { See if there's any special handling we can do based on the intrinsic code }
  4193. if (intrinsiccode <> Default(TInlineNumber)) then
  4194. result := handle_compilerproc
  4195. else
  4196. result := nil;
  4197. end;
  4198. procedure tcallnode.order_parameters;
  4199. var
  4200. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  4201. currloc : tcgloc;
  4202. indexcount: Integer;
  4203. begin
  4204. indexcount:=0;
  4205. hpfirst:=nil;
  4206. hpcurr:=tcallparanode(left);
  4207. { cache all info about parameters containing stack tainting calls,
  4208. since we will need it a lot below and calculting it can be expensive }
  4209. while assigned(hpcurr) do
  4210. begin
  4211. { Also remember the original parameter order for the sake of
  4212. tcallnode.simplify }
  4213. if hpcurr.originalindex = -1 then
  4214. begin
  4215. hpcurr.originalindex := indexcount;
  4216. Inc(indexcount);
  4217. end;
  4218. hpcurr.init_contains_stack_tainting_call_cache;
  4219. hpcurr:=tcallparanode(hpcurr.right);
  4220. end;
  4221. hpcurr:=tcallparanode(left);
  4222. while assigned(hpcurr) do
  4223. begin
  4224. { pull out }
  4225. hpnext:=tcallparanode(hpcurr.right);
  4226. { pull in at the correct place.
  4227. Used order:
  4228. 1. vs_out for a reference-counted type
  4229. 2. LOC_REFERENCE with smallest offset (i386 only)
  4230. 3. LOC_REFERENCE with least complexity (non-i386 only)
  4231. 4. LOC_REFERENCE with most complexity (non-i386 only)
  4232. 5. LOC_REGISTER with most complexity
  4233. 6. LOC_REGISTER with least complexity
  4234. For the moment we only look at the first parameter field. Combining it
  4235. with multiple parameter fields will make things a lot complexer (PFV)
  4236. The reason for the difference regarding complexity ordering
  4237. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  4238. we first want to treat the LOC_REFERENCE destinations whose
  4239. calculation does not require a call, because their location
  4240. may contain registers which might otherwise have to be saved
  4241. if a call has to be evaluated first. The calculated value is
  4242. stored on the stack and will thus no longer occupy any
  4243. register.
  4244. Similarly, for the register parameters we first want to
  4245. evaluate the calls, because otherwise the already loaded
  4246. register parameters will have to be saved so the intermediate
  4247. call can be evaluated (JM) }
  4248. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  4249. internalerror(200412152);
  4250. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  4251. hpprev:=nil;
  4252. hp:=hpfirst;
  4253. { on fixed_stack targets, always evaluate parameters containing
  4254. a call with stack parameters before all other parameters,
  4255. because they will prevent any other parameters from being put
  4256. in their final place; if both the current and the next para
  4257. contain a stack tainting call, don't do anything to prevent
  4258. them from keeping on chasing eachother's tail }
  4259. while assigned(hp) do
  4260. begin
  4261. if paramanager.use_fixed_stack and
  4262. hpcurr.contains_stack_tainting_call_cached then
  4263. break;
  4264. case currloc of
  4265. LOC_REFERENCE :
  4266. begin
  4267. case hp.parasym.paraloc[callerside].location^.loc of
  4268. LOC_REFERENCE :
  4269. begin
  4270. { Offset is calculated like:
  4271. sub esp,12
  4272. mov [esp+8],para3
  4273. mov [esp+4],para2
  4274. mov [esp],para1
  4275. call function
  4276. That means the for pushes the para with the
  4277. highest offset (see para3) needs to be pushed first
  4278. }
  4279. {$if defined(i386) or defined(i8086) or defined(m68k) or defined(z80)}
  4280. { the i386, i8086, m68k, z80 and jvm code generators expect all reference }
  4281. { parameters to be in this order so they can use }
  4282. { pushes in case of no fixed stack }
  4283. if (not paramanager.use_fixed_stack and
  4284. (hpcurr.parasym.paraloc[callerside].location^.reference.offset>
  4285. hp.parasym.paraloc[callerside].location^.reference.offset)) or
  4286. (paramanager.use_fixed_stack and
  4287. (node_complexity(hpcurr.left)<node_complexity(hp.left))) then
  4288. {$elseif defined(jvm) or defined(wasm)}
  4289. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset<hp.parasym.paraloc[callerside].location^.reference.offset) then
  4290. {$else jvm}
  4291. if (node_complexity(hpcurr.left)<node_complexity(hp.left)) then
  4292. {$endif jvm}
  4293. break;
  4294. end;
  4295. LOC_MMREGISTER,
  4296. LOC_REGISTER,
  4297. LOC_FPUREGISTER :
  4298. break;
  4299. else
  4300. ;
  4301. end;
  4302. end;
  4303. LOC_MMREGISTER,
  4304. LOC_FPUREGISTER,
  4305. LOC_REGISTER :
  4306. begin
  4307. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  4308. (node_complexity(hpcurr.left)>node_complexity(hp.left)) then
  4309. break;
  4310. end;
  4311. else
  4312. ;
  4313. end;
  4314. hpprev:=hp;
  4315. hp:=tcallparanode(hp.right);
  4316. end;
  4317. hpcurr.right:=hp;
  4318. if assigned(hpprev) then
  4319. hpprev.right:=hpcurr
  4320. else
  4321. hpfirst:=hpcurr;
  4322. { next }
  4323. hpcurr:=hpnext;
  4324. end;
  4325. left:=hpfirst;
  4326. { now mark each parameter that is followed by a stack-tainting call,
  4327. to determine on use_fixed_stack targets which ones can immediately be
  4328. put in their final destination. Unforunately we can never put register
  4329. parameters immediately in their final destination (even on register-
  4330. rich architectures such as the PowerPC), because the code generator
  4331. can still insert extra calls that only make use of register
  4332. parameters (fpc_move() etc. }
  4333. hpcurr:=hpfirst;
  4334. while assigned(hpcurr) do
  4335. begin
  4336. if hpcurr.contains_stack_tainting_call_cached then
  4337. begin
  4338. { all parameters before this one are followed by a stack
  4339. tainting call }
  4340. hp:=hpfirst;
  4341. while hp<>hpcurr do
  4342. begin
  4343. hp.ffollowed_by_stack_tainting_call_cached:=true;
  4344. hp:=tcallparanode(hp.right);
  4345. end;
  4346. hpfirst:=hpcurr;
  4347. end;
  4348. hpcurr:=tcallparanode(hpcurr.right);
  4349. end;
  4350. end;
  4351. procedure tcallnode.check_stack_parameters;
  4352. var
  4353. hp : tcallparanode;
  4354. loc : pcgparalocation;
  4355. begin
  4356. hp:=tcallparanode(left);
  4357. while assigned(hp) do
  4358. begin
  4359. if assigned(hp.parasym) then
  4360. begin
  4361. loc:=hp.parasym.paraloc[callerside].location;
  4362. while assigned(loc) do
  4363. begin
  4364. if loc^.loc=LOC_REFERENCE then
  4365. begin
  4366. include(current_procinfo.flags,pi_has_stackparameter);
  4367. exit;
  4368. end;
  4369. loc:=loc^.next;
  4370. end;
  4371. end;
  4372. hp:=tcallparanode(hp.right);
  4373. end;
  4374. end;
  4375. function tcallnode.heuristics_favors_inlining:boolean;
  4376. var
  4377. limExcluding: cardinal;
  4378. begin
  4379. { Prevent too deep inlining recursion and code bloat by inlining
  4380. The actual formuala is
  4381. inlinelevel/3+1 /-------
  4382. node count < -----------------\/ 10000
  4383. This allows exponential grow of the code only to a certain limit.
  4384. Remarks
  4385. - The current approach calculates the inlining level top down, so outer call nodes (nodes closer to the leaf) might not be inlined
  4386. if the max. complexity is reached. This is done because it makes the implementation easier and because
  4387. there might be situations were it is more beneficial to inline inner nodes and do the calls to the outer nodes
  4388. if the outer nodes are in a seldomly used code path
  4389. - The code avoids to use functions from the math unit
  4390. }
  4391. limExcluding:=round(exp((1.0/(inlinelevel/3.0+1))*ln(10000)));
  4392. result:=node_count(tprocdef(procdefinition).inlininginfo^.code,limExcluding)<limExcluding;
  4393. end;
  4394. procedure tcallnode.check_inlining;
  4395. var
  4396. st : tsymtable;
  4397. para : tcallparanode;
  4398. begin
  4399. { Can we inline the procedure? }
  4400. if (po_inline in procdefinition.procoptions) and
  4401. (procdefinition.typ=procdef) and
  4402. tprocdef(procdefinition).has_inlininginfo and
  4403. heuristics_favors_inlining then
  4404. begin
  4405. include(callnodeflags,cnf_do_inline);
  4406. { Check if we can inline the procedure when it references proc/var that
  4407. are not in the globally available }
  4408. st:=procdefinition.owner;
  4409. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  4410. st:=st.defowner.owner;
  4411. if not(tf_supports_hidden_symbols in target_info.flags) and
  4412. (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  4413. (st.symtabletype=globalsymtable) and
  4414. (not st.iscurrentunit) then
  4415. begin
  4416. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references private symbols from other unit');
  4417. exclude(callnodeflags,cnf_do_inline);
  4418. end;
  4419. para:=tcallparanode(parameters);
  4420. while assigned(para) do
  4421. begin
  4422. if not para.can_be_inlined then
  4423. begin
  4424. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+
  4425. '", invocation parameter contains an unsafe/unsupported construct');
  4426. exclude(callnodeflags,cnf_do_inline);
  4427. break;
  4428. end;
  4429. para:=tcallparanode(para.nextpara);
  4430. end;
  4431. end;
  4432. end;
  4433. function tcallnode.pass_1 : tnode;
  4434. procedure mark_unregable_parameters;
  4435. var
  4436. hp : tcallparanode;
  4437. begin
  4438. hp:=tcallparanode(left);
  4439. while assigned(hp) do
  4440. begin
  4441. do_typecheckpass(hp.left);
  4442. { When the address needs to be pushed then the register is
  4443. not regable. Exception is when the location is also a var
  4444. parameter and we can pass the address transparently (but
  4445. that is handled by make_not_regable if ra_addr_regable is
  4446. passed, and make_not_regable always needs to called for
  4447. the ra_addr_taken info for non-invisble parameters) }
  4448. if (not (cpf_varargs_para in hp.callparaflags)) and (
  4449. not(
  4450. (vo_is_hidden_para in hp.parasym.varoptions) and
  4451. (hp.left.resultdef.typ in [pointerdef,classrefdef])
  4452. ) and
  4453. paramanager.push_addr_param(hp.parasym.varspez,hp.parasym.vardef,
  4454. self.procdefinition.proccalloption)
  4455. ) then
  4456. { pushing the address of a variable to take the place of a temp
  4457. as the complex function result of a function does not make its
  4458. address escape the current block, as the "address of the
  4459. function result" is not something which can be stored
  4460. persistently by the callee (it becomes invalid when the callee
  4461. returns) }
  4462. if not(vo_is_funcret in hp.parasym.varoptions) and
  4463. ((po_inline in procdefinition.procoptions) or
  4464. (not(po_compilerproc in procdefinition.procoptions) and
  4465. (hp.parasym.varspez=vs_const))
  4466. ) then
  4467. make_not_regable(hp.left,[ra_addr_regable,ra_addr_taken])
  4468. else
  4469. make_not_regable(hp.left,[ra_addr_regable]);
  4470. hp:=tcallparanode(hp.right);
  4471. end;
  4472. end;
  4473. var
  4474. para: tcallparanode;
  4475. oldcallnode: tcallnode;
  4476. begin
  4477. result:=nil;
  4478. oldcallnode:=aktcallnode;
  4479. aktcallnode:=self;
  4480. try
  4481. { as pass_1 is never called on the methodpointer node, we must check
  4482. here that it's not a helper type }
  4483. if assigned(methodpointer) and
  4484. (methodpointer.nodetype=typen) and
  4485. is_objectpascal_helper(ttypenode(methodpointer).typedef) and
  4486. not ttypenode(methodpointer).helperallowed then
  4487. begin
  4488. CGMessage(parser_e_no_category_as_types);
  4489. { we get an internal error when trying to insert the hidden
  4490. parameters in this case }
  4491. exit;
  4492. end;
  4493. { can we get rid of the call? }
  4494. if (cs_opt_remove_empty_proc in current_settings.optimizerswitches) and
  4495. not(cnf_return_value_used in callnodeflags) and
  4496. (procdefinition.typ=procdef) and
  4497. tprocdef(procdefinition).isempty and
  4498. { allow only certain proc options }
  4499. ((tprocdef(procdefinition).procoptions-[po_none,po_classmethod,po_staticmethod,
  4500. po_interrupt,po_iocheck,po_assembler,po_msgstr,po_msgint,po_exports,po_external,po_overload,
  4501. po_nostackframe,po_has_mangledname,po_has_public_name,po_forward,po_global,
  4502. po_inline,po_compilerproc,po_has_importdll,po_has_importname,po_kylixlocal,po_dispid,po_delphi_nested_cc,
  4503. po_rtlproc,po_ignore_for_overload_resolution,po_auto_raised_visibility])=[]) then
  4504. begin
  4505. { check parameters for side effects }
  4506. para:=tcallparanode(left);
  4507. while assigned(para) do
  4508. begin
  4509. if (para.parasym.typ = paravarsym) and
  4510. ((para.parasym.refs>0) or
  4511. { array of consts are converted later on so we need to skip them here
  4512. else no error detection is done }
  4513. is_array_of_const(para.parasym.vardef) or
  4514. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4515. might_have_sideeffects(para.left)) then
  4516. break;
  4517. para:=tcallparanode(para.right);
  4518. end;
  4519. { finally, remove it if no parameter with side effect has been found }
  4520. if para=nil then
  4521. begin
  4522. result:=cnothingnode.create;
  4523. exit;
  4524. end;
  4525. end;
  4526. { convert Objective-C calls into a message call }
  4527. if (procdefinition.typ=procdef) and
  4528. (po_objc in tprocdef(procdefinition).procoptions) then
  4529. begin
  4530. if not(cnf_objc_processed in callnodeflags) then
  4531. objc_convert_to_message_send;
  4532. end
  4533. else
  4534. begin
  4535. { The following don't apply to obj-c: obj-c methods can never be
  4536. inlined because they're always virtual and the destination can
  4537. change at run, and for the same reason we also can't perform
  4538. WPO on them (+ they have no constructors) }
  4539. { Check if the call can be inlined, sets the cnf_do_inline flag }
  4540. check_inlining;
  4541. { must be called before maybe_load_in_temp(methodpointer), because
  4542. it converts the methodpointer into a temp in case it's a call
  4543. (and we want to know the original call)
  4544. }
  4545. register_created_object_types;
  4546. end;
  4547. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  4548. is a calln this is even required to not execute the calln twice.
  4549. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  4550. calln to a loadn (PFV) }
  4551. if assigned(methodpointer) then
  4552. maybe_load_in_temp(methodpointer);
  4553. if assigned(right) and (right.resultdef.typ=procvardef) and
  4554. not tabstractprocdef(right.resultdef).is_addressonly then
  4555. maybe_load_in_temp(right);
  4556. { the return value might be stored on the current stack by allocating a temp. }
  4557. if not(paramanager.ret_in_param(procdefinition.returndef,procdefinition)) then
  4558. inc(current_procinfo.estimatedtempsize,procdefinition.returndef.size);
  4559. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  4560. maybe_create_funcret_node;
  4561. { Insert the self,vmt,function result in the parameters }
  4562. gen_hidden_parameters;
  4563. { Remove useless nodes from init/final blocks }
  4564. { (simplify depends on typecheck info) }
  4565. if assigned(callinitblock) then
  4566. begin
  4567. typecheckpass(tnode(callinitblock));
  4568. doinlinesimplify(tnode(callinitblock));
  4569. end;
  4570. if assigned(callcleanupblock) then
  4571. begin
  4572. typecheckpass(tnode(callcleanupblock));
  4573. doinlinesimplify(tnode(callcleanupblock));
  4574. end;
  4575. { If a constructor calls another constructor of the same or of an
  4576. inherited class, some targets (jvm) have to generate different
  4577. entry code for the constructor. }
  4578. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  4579. (procdefinition.typ=procdef) and
  4580. (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  4581. ([cnf_member_call,cnf_inherited] * callnodeflags <> []) then
  4582. current_procinfo.ConstructorCallingConstructor:=true;
  4583. { Continue with checking a normal call or generate the inlined code }
  4584. if cnf_do_inline in callnodeflags then
  4585. result:=pass1_inline
  4586. else
  4587. begin
  4588. if (po_inline in procdefinition.procoptions) and not(po_compilerproc in procdefinition.procoptions) and
  4589. (procdefinition.typ=procdef) and
  4590. not (pio_inline_not_possible in tprocdef(procdefinition).implprocoptions) then
  4591. begin
  4592. Message1(cg_n_no_inline,tprocdef(procdefinition).customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker, pno_prettynames]));
  4593. end;
  4594. mark_unregable_parameters;
  4595. result:=pass1_normal;
  4596. end;
  4597. finally
  4598. aktcallnode:=oldcallnode;
  4599. end;
  4600. end;
  4601. function tcallnode.pass1_normal : tnode;
  4602. begin
  4603. result:=nil;
  4604. { calculate the parameter info for the procdef }
  4605. procdefinition.init_paraloc_info(callerside);
  4606. { calculate the parameter size needed for this call include varargs if they are available }
  4607. if assigned(varargsparas) then
  4608. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,callerside,varargsparas)
  4609. else
  4610. pushedparasize:=procdefinition.callerargareasize;
  4611. { record maximum parameter size used in this proc }
  4612. current_procinfo.allocate_push_parasize(pushedparasize);
  4613. { check for stacked parameters }
  4614. if assigned(left) and
  4615. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  4616. check_stack_parameters;
  4617. if assigned(callinitblock) then
  4618. firstpass(tnode(callinitblock));
  4619. { function result node (tempref or simple load) }
  4620. if assigned(funcretnode) then
  4621. firstpass(funcretnode);
  4622. { parameters }
  4623. if assigned(left) then
  4624. tcallparanode(left).firstcallparan;
  4625. { procedure variable ? }
  4626. if assigned(right) then
  4627. firstpass(right);
  4628. if assigned(methodpointer) and
  4629. (methodpointer.nodetype<>typen) then
  4630. firstpass(methodpointer);
  4631. if assigned(callcleanupblock) then
  4632. firstpass(tnode(callcleanupblock));
  4633. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  4634. include(current_procinfo.flags,pi_do_call);
  4635. { order parameters }
  4636. order_parameters;
  4637. { get a register for the return value }
  4638. if (not is_void(resultdef)) then
  4639. begin
  4640. if paramanager.ret_in_param(resultdef,procdefinition) then
  4641. begin
  4642. expectloc:=LOC_REFERENCE;
  4643. end
  4644. else
  4645. { ansi/widestrings must be registered, so we can dispose them }
  4646. if is_ansistring(resultdef) or
  4647. is_widestring(resultdef) or
  4648. is_unicodestring(resultdef) then
  4649. begin
  4650. expectloc:=LOC_REFERENCE;
  4651. end
  4652. else
  4653. { we have only to handle the result if it is used }
  4654. if (cnf_return_value_used in callnodeflags) then
  4655. expectloc:=get_expect_loc
  4656. else
  4657. expectloc:=LOC_VOID;
  4658. end
  4659. else
  4660. expectloc:=LOC_VOID;
  4661. { create tree for VMT entry if required }
  4662. gen_vmt_entry_load;
  4663. end;
  4664. {$ifdef state_tracking}
  4665. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  4666. var hp:Tcallparanode;
  4667. value:Tnode;
  4668. begin
  4669. track_state_pass:=false;
  4670. hp:=Tcallparanode(left);
  4671. while assigned(hp) do
  4672. begin
  4673. if left.track_state_pass(exec_known) then
  4674. begin
  4675. left.resultdef:=nil;
  4676. do_typecheckpass(left);
  4677. end;
  4678. value:=aktstate.find_fact(hp.left);
  4679. if value<>nil then
  4680. begin
  4681. track_state_pass:=true;
  4682. hp.left.free;
  4683. hp.left:=value.getcopy;
  4684. do_typecheckpass(hp.left);
  4685. end;
  4686. hp:=Tcallparanode(hp.right);
  4687. end;
  4688. end;
  4689. {$endif}
  4690. {**************************************************************************
  4691. INLINING SUPPORT
  4692. **************************************************************************}
  4693. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  4694. var
  4695. paras: tcallparanode;
  4696. temp: tnode;
  4697. indexnr : integer;
  4698. begin
  4699. result := fen_false;
  4700. n.fileinfo := pfileposinfo(arg)^;
  4701. if (n.nodetype = loadn) then
  4702. begin
  4703. case tloadnode(n).symtableentry.typ of
  4704. paravarsym :
  4705. begin
  4706. paras := tcallparanode(left);
  4707. while assigned(paras) and
  4708. (paras.parasym <> tloadnode(n).symtableentry) do
  4709. paras := tcallparanode(paras.right);
  4710. if assigned(paras) then
  4711. begin
  4712. temp:=paras.left.getcopy;
  4713. { inherit modification information, this is needed by the dfa/cse }
  4714. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4715. n.free;
  4716. n:=temp;
  4717. typecheckpass(n);
  4718. result := fen_true;
  4719. end;
  4720. end;
  4721. localvarsym :
  4722. begin
  4723. { local? }
  4724. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  4725. exit;
  4726. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  4727. if (indexnr >= inlinelocals.count) or
  4728. not assigned(inlinelocals[indexnr]) then
  4729. internalerror(20040720);
  4730. temp := tnode(inlinelocals[indexnr]).getcopy;
  4731. { inherit modification information, this is needed by the dfa/cse }
  4732. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4733. n.free;
  4734. n:=temp;
  4735. typecheckpass(n);
  4736. result := fen_true;
  4737. end;
  4738. else
  4739. ;
  4740. end;
  4741. end;
  4742. end;
  4743. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  4744. var
  4745. tempnode: ttempcreatenode;
  4746. indexnr : integer;
  4747. begin
  4748. if (TSym(p).typ <> localvarsym) then
  4749. exit;
  4750. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  4751. if (indexnr >= inlinelocals.count) then
  4752. inlinelocals.count:=indexnr+10;
  4753. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  4754. begin
  4755. if not assigned(funcretnode) then
  4756. internalerror(200709081);
  4757. inlinelocals[indexnr] := funcretnode.getcopy
  4758. end
  4759. else
  4760. begin
  4761. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,
  4762. tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  4763. addstatement(inlineinitstatement,tempnode);
  4764. if localvartrashing <> -1 then
  4765. cnodeutils.maybe_trash_variable(inlineinitstatement,tabstractnormalvarsym(p),ctemprefnode.create(tempnode));
  4766. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4767. { inherit addr_taken flag }
  4768. if (tabstractvarsym(p).addr_taken) then
  4769. tempnode.includetempflag(ti_addr_taken);
  4770. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  4771. end;
  4772. end;
  4773. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  4774. begin
  4775. result := fen_false;
  4776. { this is just to play it safe, there are more safe situations }
  4777. if (n.nodetype = derefn) or
  4778. ((n.nodetype = loadn) and
  4779. { can be nil in case of internally generated labels like $raiseaddr }
  4780. assigned(tloadnode(n).symtable) and
  4781. { globals and fields of (possibly global) objects could always be changed in the callee }
  4782. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  4783. { statics can only be modified by functions in the same unit }
  4784. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  4785. (tloadnode(n).symtable = TSymtable(arg))) or
  4786. { if the addr of the symbol is taken somewhere, it can be also non-local }
  4787. ((tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  4788. (tabstractvarsym(tloadnode(n).symtableentry).addr_taken))
  4789. )
  4790. ) or
  4791. ((n.nodetype = subscriptn) and
  4792. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  4793. result := fen_norecurse_true;
  4794. end;
  4795. function tcallnode.paraneedsinlinetemp(para: tcallparanode; const pushconstaddr, complexpara: boolean): boolean;
  4796. begin
  4797. { if it's an assignable call-by-reference parameter, we cannot pass a
  4798. temp since then the modified valua will be lost }
  4799. if para.parasym.varspez in [vs_var,vs_out] then
  4800. exit(false);
  4801. { We cannot create a formaldef temp and assign something to it }
  4802. if para.parasym.vardef.typ=formaldef then
  4803. exit(false);
  4804. { We don't need temps for parameters that are already temps, except if
  4805. the passed temp could be put in a regvar while the parameter inside
  4806. the routine cannot be (e.g., because its address is taken in the
  4807. routine), or if the temp is a const and the parameter gets modified }
  4808. if (para.left.nodetype=temprefn) and
  4809. (not(ti_may_be_in_reg in ttemprefnode(para.left).tempflags) or
  4810. not(tparavarsym(para.parasym).varregable in [vr_none,vr_addr])) and
  4811. (not(ti_const in ttemprefnode(para.left).tempflags) or
  4812. (tparavarsym(para.parasym).varstate in [vs_initialised,vs_declared,vs_read])) then
  4813. exit(false);
  4814. { We need a temp if the passed value will not be in memory, while
  4815. the parameter inside the routine must be in memory }
  4816. if (tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  4817. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  4818. exit(true);
  4819. { We try to handle complex expressions later by taking their address
  4820. and storing this address in a temp (which is then dereferenced when
  4821. the value is used; that doesn't work if we cannot take the address
  4822. of the expression though, in which case we store the result of the
  4823. expression in a temp }
  4824. if (complexpara and not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) or
  4825. (complexpara and
  4826. (not valid_for_addr(para.left,false) or
  4827. (para.left.nodetype=calln) or
  4828. is_constnode(para.left)))) then
  4829. exit(true);
  4830. { Normally, we do not need to create a temp for value parameters that
  4831. are not modified in the inlined function, and neither for const
  4832. parameters that are passed by value.
  4833. However, if we pass a global variable, an object field, a variable
  4834. whose address has been taken, or an expression containing a pointer
  4835. dereference as parameter, this value could be modified in other ways
  4836. as well (even inside the callee) and in such cases we still create a
  4837. temp to be on the safe side.
  4838. We *must not* create a temp for global variables passed by
  4839. reference to a const parameter, because if not inlined then any
  4840. changes to the original value will also be visible in the callee
  4841. (although this is technically undefined behaviour, since with
  4842. "const" the programmer tells the compiler this argument will not
  4843. change). }
  4844. if (((para.parasym.varspez=vs_value) and
  4845. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  4846. ((para.parasym.varspez=vs_const) and
  4847. not pushconstaddr)) and
  4848. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc)) then
  4849. exit(true);
  4850. { Value parameters of which we know they are modified by definition
  4851. have to be copied to a temp }
  4852. if (para.parasym.varspez=vs_value) and
  4853. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) then
  4854. exit(true);
  4855. { the compiler expects that it can take the address of parameters passed by reference in
  4856. the case of const so we can't replace the node simply by a constant node
  4857. When playing with this code, ensure that
  4858. function f(const a,b : longint) : longint;inline;
  4859. begin
  4860. result:=a*b;
  4861. end;
  4862. [...]
  4863. ...:=f(10,20));
  4864. [...]
  4865. is still folded. (FK)
  4866. }
  4867. if (para.parasym.varspez=vs_const) and
  4868. { const para's can get vs_readwritten if their address is taken ->
  4869. in case they are not passed by reference, to keep the same
  4870. behaviour as without inlining we have to make a copy in case the
  4871. originally passed parameter value gets changed inside the callee
  4872. }
  4873. (not pushconstaddr and
  4874. (para.parasym.varstate=vs_readwritten)
  4875. ) or
  4876. { call-by-reference const's may need to be passed by reference to
  4877. function called in the inlined code }
  4878. (pushconstaddr and
  4879. not valid_for_addr(para.left,false)) then
  4880. exit(true);
  4881. { insert value parameters directly if they are complex instead
  4882. of inserting a reference to the temp.
  4883. - this keeps the node tree simpler
  4884. - alignment is propagated }
  4885. if (para.parasym.varspez=vs_value) and
  4886. complexpara then
  4887. exit(true);
  4888. result:=false;
  4889. end;
  4890. function tcallnode.maybecreateinlineparatemp(para: tcallparanode; out complexpara: boolean): boolean;
  4891. var
  4892. tempnode: ttempcreatenode;
  4893. realtarget: tnode;
  4894. paracomplexity: longint;
  4895. pushconstaddr: boolean;
  4896. begin
  4897. result:=false;
  4898. { determine how a parameter is passed to the inlined body
  4899. There are three options:
  4900. - insert the node tree of the callparanode directly
  4901. If a parameter is used only once, this is the best option if we can do so
  4902. - get the address of the argument, store it in a temp and insert a dereference to this temp
  4903. If the node tree cannot be inserted directly, taking the address of the argument and using it
  4904. is the second best option, but even this is not always possible
  4905. - assign the value of the argument to a newly created temp
  4906. This is the fall back which works always
  4907. Notes:
  4908. - we need to take care that we use the type of the defined parameter and not of the
  4909. passed parameter, because these can be different in case of a formaldef (PFV)
  4910. }
  4911. { pre-compute some values }
  4912. paracomplexity:=node_complexity(para.left);
  4913. if para.parasym.varspez=vs_const then
  4914. pushconstaddr:=paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption)
  4915. else
  4916. pushconstaddr:=false;
  4917. realtarget:=actualtargetnode(@para.left)^;
  4918. { if the parameter is "complex", try to take the address of the
  4919. parameter expression, store it in a temp and replace occurrences of
  4920. the parameter with dereferencings of this temp
  4921. }
  4922. complexpara:=
  4923. { don't create a temp. for function results }
  4924. not(nf_is_funcret in realtarget.flags) and
  4925. { this makes only sense if the parameter is reasonably complex,
  4926. otherwise inserting directly is a better solution }
  4927. (
  4928. (paracomplexity>2) or
  4929. { don't create a temp. for the often seen case that p^ is passed to a var parameter }
  4930. ((paracomplexity>1) and
  4931. not((realtarget.nodetype=derefn) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) and
  4932. not((realtarget.nodetype=loadn) and tloadnode(realtarget).is_addr_param_load) and
  4933. not(realtarget.nodetype=realconstn)
  4934. )
  4935. );
  4936. { check if we have to create a temp, assign the parameter's
  4937. contents to that temp and then substitute the parameter
  4938. with the temp everywhere in the function }
  4939. if paraneedsinlinetemp(para,pushconstaddr,complexpara) then
  4940. begin
  4941. tempnode:=ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,
  4942. tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  4943. addstatement(inlineinitstatement,tempnode);
  4944. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4945. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4946. para.left));
  4947. para.left := ctemprefnode.create(tempnode);
  4948. { inherit addr_taken flag }
  4949. if (tabstractvarsym(para.parasym).addr_taken) then
  4950. tempnode.includetempflag(ti_addr_taken);
  4951. { inherit const }
  4952. if tabstractvarsym(para.parasym).varspez=vs_const then
  4953. begin
  4954. tempnode.includetempflag(ti_const);
  4955. { apply less strict rules for the temp. to be a register than
  4956. ttempcreatenode does
  4957. this way, dyn. array, ansistrings etc. can be put into registers as well }
  4958. if tparavarsym(para.parasym).is_regvar(false) then
  4959. tempnode.includetempflag(ti_may_be_in_reg);
  4960. end;
  4961. result:=true;
  4962. end
  4963. { for formaldefs, we do not need a temp., but it must be inherited if they are not regable }
  4964. else if (para.parasym.vardef.typ=formaldef) and not(tparavarsym(para.parasym).is_regvar(false)) then
  4965. make_not_regable(para.left,[ra_addr_regable]);
  4966. end;
  4967. procedure tcallnode.createinlineparas;
  4968. var
  4969. para: tcallparanode;
  4970. n: tnode;
  4971. complexpara: boolean;
  4972. begin
  4973. { parameters }
  4974. para := tcallparanode(left);
  4975. while assigned(para) do
  4976. begin
  4977. if (para.parasym.typ = paravarsym) and
  4978. ((para.parasym.refs>0) or
  4979. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4980. might_have_sideeffects(para.left)) then
  4981. begin
  4982. { must take copy of para.left, because if it contains a }
  4983. { temprefn pointing to a copied temp (e.g. methodpointer), }
  4984. { then this parameter must be changed to point to the copy of }
  4985. { that temp (JM) }
  4986. n := para.left.getcopy;
  4987. para.left.free;
  4988. para.left := n;
  4989. firstpass(para.left);
  4990. if not maybecreateinlineparatemp(para,complexpara) and
  4991. complexpara then
  4992. wrapcomplexinlinepara(para);
  4993. end;
  4994. para := tcallparanode(para.right);
  4995. end;
  4996. { local variables }
  4997. if not assigned(tprocdef(procdefinition).localst) or
  4998. (tprocdef(procdefinition).localst.SymList.count = 0) then
  4999. exit;
  5000. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  5001. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  5002. end;
  5003. procedure tcallnode.wrapcomplexinlinepara(para: tcallparanode);
  5004. var
  5005. ptrtype: tdef;
  5006. tempnode: ttempcreatenode;
  5007. paraaddr: taddrnode;
  5008. isfuncretnode : boolean;
  5009. begin
  5010. ptrtype:=cpointerdef.getreusable(para.left.resultdef);
  5011. tempnode:=ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,true);
  5012. addstatement(inlineinitstatement,tempnode);
  5013. isfuncretnode:=nf_is_funcret in para.left.flags;
  5014. if isfuncretnode then
  5015. addstatement(inlinecleanupstatement,ctempdeletenode.create_normal_temp(tempnode))
  5016. else
  5017. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  5018. { inherit addr_taken flag }
  5019. if (tabstractvarsym(para.parasym).addr_taken) then
  5020. tempnode.includetempflag(ti_addr_taken);
  5021. { inherit read only }
  5022. if tabstractvarsym(para.parasym).varspez=vs_const then
  5023. tempnode.includetempflag(ti_const);
  5024. paraaddr:=caddrnode.create_internal(para.left);
  5025. include(paraaddr.addrnodeflags,anf_typedaddr);
  5026. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  5027. paraaddr));
  5028. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  5029. if isfuncretnode then
  5030. Include(para.left.flags,nf_is_funcret);
  5031. end;
  5032. function UsesTmp(var n: tnode; arg: pointer): foreachnoderesult;
  5033. begin
  5034. Result:=fen_false;
  5035. if (n.nodetype=temprefn) and (ttemprefnode(n).tempinfo=arg) then
  5036. Result:=fen_norecurse_true;
  5037. end;
  5038. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  5039. var
  5040. hp : tstatementnode;
  5041. hp2 : tnode;
  5042. resassign : tassignmentnode;
  5043. begin
  5044. result:=nil;
  5045. if not assigned(funcretnode) or
  5046. not(cnf_return_value_used in callnodeflags) then
  5047. exit;
  5048. { block already optimized? }
  5049. if not(inlineblock.nodetype=blockn) then
  5050. exit;
  5051. { tempcreatenode for the function result }
  5052. hp:=tstatementnode(inlineblock.left);
  5053. if not(assigned(hp)) or
  5054. (hp.left.nodetype <> tempcreaten) or
  5055. not(nf_is_funcret in hp.left.flags) then
  5056. exit;
  5057. hp:=tstatementnode(hp.right);
  5058. if not(assigned(hp)) or
  5059. (hp.left.nodetype<>assignn)
  5060. { FK: check commented, original comment was:
  5061. constant assignment? right must be a constant (mainly to avoid trying
  5062. to reuse local temps which may already be freed afterwards once these
  5063. checks are made looser)
  5064. or
  5065. not is_constnode(tassignmentnode(hp.left).right)
  5066. So far I found no example why removing this check might be a problem.
  5067. If this needs to be revert, issue #36279 must be checked/solved again.
  5068. }
  5069. then
  5070. exit;
  5071. { left must be function result }
  5072. resassign:=tassignmentnode(hp.left);
  5073. hp2:=resassign.left;
  5074. { can have extra type conversion due to absolute mapping
  5075. of <fucntionname> on function result var }
  5076. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  5077. hp2:=ttypeconvnode(hp2).left;
  5078. if (hp2.nodetype<>temprefn) or
  5079. { check if right references the temp. being removed, i.e. using an uninitialized result }
  5080. foreachnodestatic(resassign.right,@UsesTmp,ttemprefnode(hp2).tempinfo) or
  5081. not(nf_is_funcret in hp2.flags) then
  5082. exit;
  5083. { tempdelete to normal of the function result }
  5084. hp:=tstatementnode(hp.right);
  5085. if not(assigned(hp)) or
  5086. (hp.left.nodetype <> tempdeleten) then
  5087. exit;
  5088. { the function result once more }
  5089. hp:=tstatementnode(hp.right);
  5090. if not(assigned(hp)) or
  5091. (hp.left.nodetype<>temprefn) or
  5092. not(nf_is_funcret in hp.left.flags) then
  5093. exit;
  5094. { should be the end }
  5095. if assigned(hp.right) then
  5096. exit;
  5097. { we made it! }
  5098. result:=ctypeconvnode.create_internal(tassignmentnode(resassign).right.getcopy,hp2.resultdef);
  5099. firstpass(result);
  5100. end;
  5101. { this procedure removes the user code flag because it prevents optimizations }
  5102. function removeusercodeflag(var n : tnode; arg : pointer) : foreachnoderesult;
  5103. begin
  5104. result:=fen_false;
  5105. if nf_usercode_entry in n.flags then
  5106. begin
  5107. exclude(n.flags,nf_usercode_entry);
  5108. result:=fen_norecurse_true;
  5109. end;
  5110. end;
  5111. { reference symbols that are imported from another unit }
  5112. function importglobalsyms(var n:tnode; arg:pointer):foreachnoderesult;
  5113. var
  5114. sym : tsym;
  5115. begin
  5116. result:=fen_false;
  5117. if n.nodetype=loadn then
  5118. begin
  5119. sym:=tloadnode(n).symtableentry;
  5120. if sym.typ=staticvarsym then
  5121. begin
  5122. if FindUnitSymtable(tloadnode(n).symtable).moduleid<>current_module.moduleid then
  5123. current_module.addimportedsym(sym);
  5124. end
  5125. else if (sym.typ=constsym) and (tconstsym(sym).consttyp in [constwresourcestring,constresourcestring]) then
  5126. begin
  5127. if tloadnode(n).symtableentry.owner.moduleid<>current_module.moduleid then
  5128. current_module.addimportedsym(sym);
  5129. end;
  5130. end
  5131. else if (n.nodetype=calln) then
  5132. begin
  5133. if (assigned(tcallnode(n).procdefinition)) and
  5134. (tcallnode(n).procdefinition.typ=procdef) and
  5135. (findunitsymtable(tcallnode(n).procdefinition.owner).moduleid<>current_module.moduleid) then
  5136. current_module.addimportedsym(tprocdef(tcallnode(n).procdefinition).procsym);
  5137. end;
  5138. end;
  5139. function tcallnode.pass1_inline:tnode;
  5140. var
  5141. n,
  5142. body : tnode;
  5143. para : tcallparanode;
  5144. inlineblock,
  5145. inlinecleanupblock : tblocknode;
  5146. begin
  5147. inc(inlinelevel);
  5148. result:=nil;
  5149. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  5150. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  5151. internalerror(200412021);
  5152. inlinelocals:=TFPObjectList.create(true);
  5153. { inherit flags }
  5154. current_procinfo.flags:=current_procinfo.flags+
  5155. ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  5156. { Create new code block for inlining }
  5157. inlineblock:=internalstatements(inlineinitstatement);
  5158. { make sure that valid_for_assign() returns false for this block
  5159. (otherwise assigning values to the block will result in assigning
  5160. values to the inlined function's result) }
  5161. include(inlineblock.flags,nf_no_lvalue);
  5162. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  5163. if assigned(callinitblock) then
  5164. addstatement(inlineinitstatement,callinitblock.getcopy);
  5165. { replace complex parameters with temps }
  5166. createinlineparas;
  5167. { create a copy of the body and replace parameter loads with the parameter values }
  5168. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  5169. foreachnodestatic(pm_postprocess,body,@removeusercodeflag,nil);
  5170. foreachnodestatic(pm_postprocess,body,@importglobalsyms,nil);
  5171. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  5172. { Concat the body and finalization parts }
  5173. addstatement(inlineinitstatement,body);
  5174. addstatement(inlineinitstatement,inlinecleanupblock);
  5175. inlinecleanupblock:=nil;
  5176. if assigned(callcleanupblock) then
  5177. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  5178. { the last statement of the new inline block must return the
  5179. location and type of the function result.
  5180. This is not needed when the result is not used, also the tempnode is then
  5181. already destroyed by a tempdelete in the callcleanupblock tree }
  5182. if not is_void(resultdef) and
  5183. (cnf_return_value_used in callnodeflags) then
  5184. begin
  5185. if assigned(funcretnode) then
  5186. addstatement(inlineinitstatement,funcretnode.getcopy)
  5187. else
  5188. begin
  5189. para:=tcallparanode(left);
  5190. while assigned(para) do
  5191. begin
  5192. if (vo_is_hidden_para in para.parasym.varoptions) and
  5193. (vo_is_funcret in para.parasym.varoptions) then
  5194. begin
  5195. addstatement(inlineinitstatement,para.left.getcopy);
  5196. break;
  5197. end;
  5198. para:=tcallparanode(para.right);
  5199. end;
  5200. end;
  5201. end;
  5202. typecheckpass(tnode(inlineblock));
  5203. doinlinesimplify(tnode(inlineblock));
  5204. node_reset_flags(tnode(inlineblock),[],[tnf_pass1_done]);
  5205. firstpass(tnode(inlineblock));
  5206. result:=inlineblock;
  5207. { if the function result is used then verify that the blocknode
  5208. returns the same result type as the original callnode }
  5209. if (cnf_return_value_used in callnodeflags) and
  5210. (result.resultdef<>resultdef) then
  5211. internalerror(200709171);
  5212. { free the temps for the locals }
  5213. inlinelocals.free;
  5214. inlinelocals:=nil;
  5215. inlineinitstatement:=nil;
  5216. inlinecleanupstatement:=nil;
  5217. n:=optimize_funcret_assignment(inlineblock);
  5218. if assigned(n) then
  5219. begin
  5220. inlineblock.free;
  5221. result:=n;
  5222. end;
  5223. {$ifdef DEBUGINLINE}
  5224. writeln;
  5225. writeln('**************************',tprocdef(procdefinition).mangledname);
  5226. printnode(output,result);
  5227. {$endif DEBUGINLINE}
  5228. dec(inlinelevel);
  5229. end;
  5230. end.