ncnv.pas 203 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  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 ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnodeflag = (
  28. { the typeconvnode is a proc_2_procvar, generated internally by an
  29. address operator, such as @proc, Addr(proc), Ofs(proc) or Seg(proc),
  30. which is then going to be converted to a void pointer. Why does it
  31. matter? Because, on i8086 far code memory models you're allowed to
  32. take the address of a _near_ procedure as a void pointer (which the
  33. @ operator does in TP mode), but not as a procvar (in that case the
  34. procedure must be far). }
  35. tcnf_proc_2_procvar_2_voidpointer,
  36. { proc_2_procvar, generated internally by Ofs() }
  37. tcnf_proc_2_procvar_get_offset_only
  38. );
  39. ttypeconvnodeflags = set of ttypeconvnodeflag;
  40. ttypeconvnode = class(tunarynode)
  41. totypedef : tdef;
  42. totypedefderef : tderef;
  43. convtype : tconverttype;
  44. convnodeflags : ttypeconvnodeflags;
  45. warn_pointer_to_signed,
  46. assignment_side: boolean;
  47. constructor create(node : tnode;def:tdef);virtual;
  48. constructor create_explicit(node : tnode;def:tdef);
  49. constructor create_internal(node : tnode;def:tdef);
  50. constructor create_proc_to_procvar(node : tnode);
  51. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  52. procedure ppuwrite(ppufile:tcompilerppufile);override;
  53. procedure buildderefimpl;override;
  54. procedure derefimpl;override;
  55. function dogetcopy : tnode;override;
  56. procedure printnodeinfo(var t : text);override;
  57. {$ifdef DEBUG_NODE_XML}
  58. procedure XMLPrintNodeInfo(var T: Text); override;
  59. {$endif DEBUG_NODE_XML}
  60. function pass_1 : tnode;override;
  61. function pass_typecheck:tnode;override;
  62. function simplify(forinline : boolean):tnode; override;
  63. procedure mark_write;override;
  64. function docompare(p: tnode) : boolean; override;
  65. function retains_value_location:boolean;
  66. function assign_allowed:boolean;
  67. procedure second_call_helper(c : tconverttype);
  68. { always called before any other type conversion checks. If it
  69. returns true, the type conversion is ok and no further checks/
  70. handling are required. }
  71. function target_specific_general_typeconv: boolean;virtual;
  72. { called in case of a valid explicit type conversion. Can be used to
  73. replace this explicit type conversion with a different node, or to
  74. reject it after all }
  75. function target_specific_explicit_typeconv: boolean;virtual;
  76. { called when inserttypeconv is used to convert to a def that is equal
  77. according to compare_defs() }
  78. class function target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean; virtual;
  79. protected
  80. function typecheck_int_to_int : tnode; virtual;
  81. function typecheck_cord_to_pointer : tnode; virtual;
  82. function typecheck_chararray_to_string : tnode; virtual;
  83. function typecheck_string_to_chararray : tnode; virtual;
  84. function typecheck_string_to_string : tnode; virtual;
  85. function typecheck_char_to_string : tnode; virtual;
  86. function typecheck_char_to_chararray : tnode; virtual;
  87. function typecheck_int_to_real : tnode; virtual;
  88. function typecheck_real_to_real : tnode; virtual;
  89. function typecheck_real_to_currency : tnode; virtual;
  90. function typecheck_cchar_to_pchar : tnode; virtual;
  91. function typecheck_cstring_to_pchar : tnode; virtual;
  92. function typecheck_cstring_to_int : tnode; virtual;
  93. function typecheck_char_to_char : tnode; virtual;
  94. function typecheck_arrayconstructor_to_set : tnode; virtual;
  95. function typecheck_set_to_set : tnode; virtual;
  96. function typecheck_pchar_to_string : tnode; virtual;
  97. function typecheck_interface_to_string : tnode; virtual;
  98. function typecheck_interface_to_guid : tnode; virtual;
  99. function typecheck_dynarray_to_openarray : tnode; virtual;
  100. function typecheck_pwchar_to_string : tnode; virtual;
  101. function typecheck_variant_to_dynarray : tnode; virtual;
  102. function typecheck_dynarray_to_variant : tnode; virtual;
  103. function typecheck_variant_to_enum : tnode; virtual;
  104. function typecheck_enum_to_variant : tnode; virtual;
  105. function typecheck_proc_to_procvar : tnode; virtual;
  106. function typecheck_variant_to_interface : tnode; virtual;
  107. function typecheck_interface_to_variant : tnode; virtual;
  108. function typecheck_array_2_dynarray : tnode; virtual;
  109. function typecheck_elem_2_openarray : tnode; virtual;
  110. function typecheck_arrayconstructor_to_dynarray : tnode; virtual;
  111. function typecheck_arrayconstructor_to_array : tnode; virtual;
  112. function typecheck_anonproc_2_funcref : tnode; virtual;
  113. function typecheck_procvar_2_funcref : tnode; virtual;
  114. private
  115. function _typecheck_int_to_int : tnode;
  116. function _typecheck_cord_to_pointer : tnode;
  117. function _typecheck_chararray_to_string : tnode;
  118. function _typecheck_string_to_chararray : tnode;
  119. function _typecheck_string_to_string : tnode;
  120. function _typecheck_char_to_string : tnode;
  121. function _typecheck_char_to_chararray : tnode;
  122. function _typecheck_int_to_real : tnode;
  123. function _typecheck_real_to_real : tnode;
  124. function _typecheck_real_to_currency : tnode;
  125. function _typecheck_cchar_to_pchar : tnode;
  126. function _typecheck_cstring_to_pchar : tnode;
  127. function _typecheck_cstring_to_int : tnode;
  128. function _typecheck_char_to_char : tnode;
  129. function _typecheck_arrayconstructor_to_set : tnode;
  130. function _typecheck_set_to_set : tnode;
  131. function _typecheck_pchar_to_string : tnode;
  132. function _typecheck_interface_to_string : tnode;
  133. function _typecheck_interface_to_guid : tnode;
  134. function _typecheck_dynarray_to_openarray : tnode;
  135. function _typecheck_pwchar_to_string : tnode;
  136. function _typecheck_variant_to_dynarray : tnode;
  137. function _typecheck_dynarray_to_variant : tnode;
  138. function _typecheck_variant_to_enum : tnode;
  139. function _typecheck_enum_to_variant : tnode;
  140. function _typecheck_proc_to_procvar : tnode;
  141. function _typecheck_variant_to_interface : tnode;
  142. function _typecheck_interface_to_variant : tnode;
  143. function _typecheck_array_2_dynarray : tnode;
  144. function _typecheck_elem_2_openarray : tnode;
  145. function _typecheck_arrayconstructor_to_dynarray : tnode;
  146. function _typecheck_arrayconstructor_to_array : tnode;
  147. function _typecheck_anonproc_to_funcref : tnode;
  148. function _typecheck_procvar_to_funcref : tnode;
  149. protected
  150. function first_int_to_int : tnode;virtual;
  151. function first_cstring_to_pchar : tnode;virtual;
  152. function first_cstring_to_int : tnode;virtual;
  153. function first_string_to_chararray : tnode;virtual;
  154. function first_char_to_string : tnode;virtual;
  155. function first_char_to_chararray : tnode; virtual;
  156. function first_nothing : tnode;virtual;
  157. function first_array_to_pointer : tnode;virtual;
  158. function first_int_to_real : tnode;virtual;
  159. function first_real_to_real : tnode;virtual;
  160. function first_pointer_to_array : tnode;virtual;
  161. function first_cchar_to_pchar : tnode;virtual;
  162. function first_bool_to_int : tnode;virtual;
  163. function first_int_to_bool : tnode;virtual;
  164. function first_bool_to_bool : tnode;virtual;
  165. function first_proc_to_procvar : tnode;virtual;
  166. function first_nil_to_methodprocvar : tnode;virtual;
  167. function first_set_to_set : tnode;virtual;
  168. function first_cord_to_pointer : tnode;virtual;
  169. function first_ansistring_to_pchar : tnode;virtual;
  170. function first_arrayconstructor_to_set : tnode;virtual;
  171. function first_class_to_intf : tnode;virtual;
  172. function first_char_to_char : tnode;virtual;
  173. function first_string_to_string : tnode;virtual;
  174. function first_call_helper(c : tconverttype) : tnode;
  175. function typecheck_call_helper(c : tconverttype) : tnode;
  176. private
  177. { these wrapper are necessary, because the first_* stuff is called }
  178. { through a table. Without the wrappers override wouldn't have }
  179. { any effect }
  180. function _first_int_to_int : tnode;
  181. function _first_cstring_to_pchar : tnode;
  182. function _first_cstring_to_int : tnode;
  183. function _first_string_to_chararray : tnode;
  184. function _first_char_to_string : tnode;
  185. function _first_char_to_chararray : tnode;
  186. function _first_nothing : tnode;
  187. function _first_array_to_pointer : tnode;
  188. function _first_int_to_real : tnode;
  189. function _first_real_to_real: tnode;
  190. function _first_pointer_to_array : tnode;
  191. function _first_cchar_to_pchar : tnode;
  192. function _first_bool_to_int : tnode;
  193. function _first_int_to_bool : tnode;
  194. function _first_bool_to_bool : tnode;
  195. function _first_proc_to_procvar : tnode;
  196. function _first_nil_to_methodprocvar : tnode;
  197. function _first_cord_to_pointer : tnode;
  198. function _first_ansistring_to_pchar : tnode;
  199. function _first_arrayconstructor_to_set : tnode;
  200. function _first_class_to_intf : tnode;
  201. function _first_char_to_char : tnode;
  202. function _first_set_to_set : tnode;
  203. function _first_string_to_string : tnode;
  204. procedure _second_int_to_int;virtual;
  205. procedure _second_string_to_string;virtual;
  206. procedure _second_cstring_to_pchar;virtual;
  207. procedure _second_cstring_to_int;virtual;
  208. procedure _second_string_to_chararray;virtual;
  209. procedure _second_array_to_pointer;virtual;
  210. procedure _second_pointer_to_array;virtual;
  211. procedure _second_chararray_to_string;virtual;
  212. procedure _second_char_to_string;virtual;
  213. procedure _second_int_to_real;virtual;
  214. procedure _second_real_to_real;virtual;
  215. procedure _second_cord_to_pointer;virtual;
  216. procedure _second_proc_to_procvar;virtual;
  217. procedure _second_nil_to_methodprocvar;virtual;
  218. procedure _second_bool_to_int;virtual;
  219. procedure _second_int_to_bool;virtual;
  220. procedure _second_bool_to_bool;virtual;
  221. procedure _second_set_to_set;virtual;
  222. procedure _second_ansistring_to_pchar;virtual;
  223. procedure _second_class_to_intf;virtual;
  224. procedure _second_char_to_char;virtual;
  225. procedure _second_elem_to_openarray;virtual;
  226. procedure _second_nothing; virtual;
  227. protected
  228. procedure second_int_to_int;virtual;abstract;
  229. procedure second_string_to_string;virtual;abstract;
  230. procedure second_cstring_to_pchar;virtual;abstract;
  231. procedure second_cstring_to_int;virtual;abstract;
  232. procedure second_string_to_chararray;virtual;abstract;
  233. procedure second_array_to_pointer;virtual;abstract;
  234. procedure second_pointer_to_array;virtual;abstract;
  235. procedure second_chararray_to_string;virtual;abstract;
  236. procedure second_char_to_string;virtual;abstract;
  237. procedure second_int_to_real;virtual;abstract;
  238. procedure second_real_to_real;virtual;abstract;
  239. procedure second_cord_to_pointer;virtual;abstract;
  240. procedure second_proc_to_procvar;virtual;abstract;
  241. procedure second_nil_to_methodprocvar;virtual;abstract;
  242. procedure second_bool_to_int;virtual;abstract;
  243. procedure second_int_to_bool;virtual;abstract;
  244. procedure second_bool_to_bool;virtual;abstract;
  245. procedure second_set_to_set;virtual;abstract;
  246. procedure second_ansistring_to_pchar;virtual;abstract;
  247. procedure second_class_to_intf;virtual;abstract;
  248. procedure second_char_to_char;virtual;abstract;
  249. procedure second_elem_to_openarray;virtual;abstract;
  250. procedure second_nothing; virtual;abstract;
  251. end;
  252. ttypeconvnodeclass = class of ttypeconvnode;
  253. { common functionality of as-nodes and is-nodes }
  254. tasisnode = class(tbinarynode)
  255. protected
  256. { if non-standard usage of as-nodes is possible, targets can override
  257. this method and return true in case the conditions are fulfilled }
  258. function target_specific_typecheck: boolean;virtual;
  259. public
  260. function pass_typecheck:tnode;override;
  261. end;
  262. tasnode = class(tasisnode)
  263. { as nodes cannot be translated directly into call nodes bcause:
  264. When using -CR, explicit class typecasts are replaced with as-nodes to perform
  265. class type checking. The problem is that if a typecasted class instance is
  266. passed as a var-parameter, then you cannot replace it with a function call. So the as-node
  267. a) call the as helper to perform the type checking
  268. b) still pass the original instance as parameter to var-parameters
  269. (and in general: to return it as the result of the as-node)
  270. so the call field is required
  271. }
  272. call: tnode;
  273. constructor create(l,r : tnode);virtual;
  274. constructor create_internal(l,r : tnode);virtual;
  275. function pass_1 : tnode;override;
  276. function dogetcopy: tnode;override;
  277. function docompare(p: tnode): boolean; override;
  278. destructor destroy; override;
  279. end;
  280. tasnodeclass = class of tasnode;
  281. tisnode = class(tasisnode)
  282. constructor create(l,r : tnode);virtual;
  283. constructor create_internal(l,r : tnode);virtual;
  284. function pass_1 : tnode;override;
  285. procedure pass_generate_code;override;
  286. end;
  287. tisnodeclass = class of tisnode;
  288. var
  289. ctypeconvnode : ttypeconvnodeclass = ttypeconvnode;
  290. casnode : tasnodeclass = tasnode;
  291. cisnode : tisnodeclass=tisnode;
  292. procedure inserttypeconv(var p:tnode;def:tdef);
  293. procedure inserttypeconv_explicit(var p:tnode;def:tdef);
  294. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  295. procedure arrayconstructor_to_set(var p : tnode);inline;
  296. function arrayconstructor_to_set(p:tnode;freep:boolean):tnode;
  297. function arrayconstructor_can_be_set(p:tnode):boolean;
  298. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  299. function maybe_global_proc_to_nested(var fromnode: tnode; todef: tdef): boolean;
  300. implementation
  301. uses
  302. globtype,systems,constexp,compinnr,
  303. cutils,verbose,globals,widestr,ppu,
  304. symconst,symdef,symsym,symcpu,symtable,
  305. ncon,ncal,nset,nadd,nmem,nmat,nbas,nutils,ninl,nflw,
  306. psub,
  307. cgbase,procinfo,
  308. htypechk,blockutl,pparautl,procdefutil,pass_1,cpuinfo;
  309. {*****************************************************************************
  310. Helpers
  311. *****************************************************************************}
  312. type
  313. ttypeconvnodetype = (tct_implicit,tct_explicit,tct_internal);
  314. procedure do_inserttypeconv(var p: tnode;def: tdef; convtype: ttypeconvnodetype);
  315. begin
  316. if not assigned(p.resultdef) then
  317. begin
  318. typecheckpass(p);
  319. if codegenerror then
  320. exit;
  321. end;
  322. { don't insert superfluous type conversions, but
  323. in case of bitpacked accesses, the original type must
  324. remain too so that not too many/few bits are laoded.
  325. Also, in case the deftyp changes, don't ignore because lots of code
  326. expects that if the resultdef is set to e.g. stringdef, it remains
  327. that way (e.g., in case of Java where java_jlstring equals
  328. unicodestring according to equal_defs, but an add node for strings
  329. still expects the resultdef of the node to be a stringdef) }
  330. if equal_defs(p.resultdef,def) and
  331. (p.resultdef.typ=def.typ) and
  332. not is_bitpacked_access(p) and
  333. { result of a hardware vector node must remain a hardware
  334. vector of the same kind (will match to tc_equal with regular arrays
  335. of same dimension/eledef) }
  336. not((p.resultdef.typ=arraydef) and
  337. tarraydef(p.resultdef).is_hwvector) and
  338. ((p.blocktype=bt_const) or
  339. not ctypeconvnode.target_specific_need_equal_typeconv(p.resultdef,def)) then
  340. begin
  341. { don't replace encoded string constants to rawbytestring encoding.
  342. preserve the codepage }
  343. if not (is_rawbytestring(def) and (p.nodetype=stringconstn)) then
  344. p.resultdef:=def
  345. end
  346. else
  347. begin
  348. case convtype of
  349. tct_implicit:
  350. p:=ctypeconvnode.create(p,def);
  351. tct_explicit:
  352. p:=ctypeconvnode.create_explicit(p,def);
  353. tct_internal:
  354. p:=ctypeconvnode.create_internal(p,def);
  355. end;
  356. p.fileinfo:=ttypeconvnode(p).left.fileinfo;
  357. typecheckpass(p);
  358. end;
  359. end;
  360. procedure inserttypeconv(var p:tnode;def:tdef);
  361. begin
  362. do_inserttypeconv(p,def,tct_implicit);
  363. end;
  364. procedure inserttypeconv_explicit(var p: tnode; def: tdef);
  365. begin
  366. do_inserttypeconv(p,def,tct_explicit);
  367. end;
  368. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  369. begin
  370. do_inserttypeconv(p,def,tct_internal);
  371. end;
  372. {*****************************************************************************
  373. Array constructor to Set Conversion
  374. *****************************************************************************}
  375. procedure arrayconstructor_to_set(var p : tnode);
  376. begin
  377. p:=arrayconstructor_to_set(p,true);
  378. end;
  379. function arrayconstructor_to_set(p:tnode;freep:boolean):tnode;
  380. var
  381. constp : tsetconstnode;
  382. p2,p3,p4 : tnode;
  383. hdef : tdef;
  384. constset : Pconstset;
  385. constsetlo,
  386. constsethi : TConstExprInt;
  387. procedure update_constsethi(def:tdef; maybetruncenumrange: boolean);
  388. begin
  389. if (def.typ=orddef) and
  390. ((torddef(def).high>=constsethi) or
  391. (torddef(def).low <=constsetlo)) then
  392. begin
  393. if torddef(def).ordtype=uwidechar then
  394. begin
  395. constsethi:=255;
  396. constsetlo:=0;
  397. if hdef=nil then
  398. hdef:=def;
  399. end
  400. else
  401. begin
  402. if (torddef(def).high>=constsethi) then
  403. constsethi:=torddef(def).high;
  404. if (torddef(def).low<=constsetlo) then
  405. constsetlo:=torddef(def).low;
  406. if hdef=nil then
  407. begin
  408. if (constsethi>255) or
  409. (torddef(def).low<0) then
  410. hdef:=u8inttype
  411. else
  412. hdef:=def;
  413. end;
  414. if constsethi>255 then
  415. constsethi:=255;
  416. if constsetlo<0 then
  417. constsetlo:=0;
  418. end;
  419. end
  420. else if (def.typ=enumdef) and
  421. ((tenumdef(def).max>=constsethi) or
  422. (tenumdef(def).min<=constsetlo)) then
  423. begin
  424. if hdef=nil then
  425. hdef:=def;
  426. if (tenumdef(def).max>=constsethi) then
  427. constsethi:=tenumdef(def).max;
  428. if (tenumdef(def).min<=constsetlo) then
  429. constsetlo:=tenumdef(def).min;
  430. { for constant set elements, delphi allows the usage of elements of enumerations which
  431. have value>255 if there is no element with a value > 255 used }
  432. if (maybetruncenumrange) then
  433. begin
  434. if constsethi>255 then
  435. constsethi:=255;
  436. if constsetlo<0 then
  437. constsetlo:=0;
  438. end;
  439. end;
  440. end;
  441. procedure do_set(pos : longint);
  442. begin
  443. if (pos and not $ff)<>0 then
  444. begin
  445. Message(parser_e_illegal_set_expr);
  446. exit;
  447. end;
  448. if pos>constsethi then
  449. constsethi:=pos;
  450. if pos<constsetlo then
  451. constsetlo:=pos;
  452. if pos in constset^ then
  453. Message(parser_e_illegal_set_expr);
  454. include(constset^,pos);
  455. end;
  456. var
  457. l : Longint;
  458. lr,hr : TConstExprInt;
  459. hp : tarrayconstructornode;
  460. oldfilepos: tfileposinfo;
  461. first: Boolean;
  462. begin
  463. { keep in sync with arrayconstructor_can_be_set }
  464. if p.nodetype<>arrayconstructorn then
  465. internalerror(200205105);
  466. new(constset);
  467. constset^:=[];
  468. hdef:=nil;
  469. { make sure to set constsetlo correctly for empty sets }
  470. if assigned(tarrayconstructornode(p).left) then
  471. constsetlo:=high(aint)
  472. else
  473. constsetlo:=0;
  474. constsethi:=0;
  475. constp:=csetconstnode.create(nil,hdef);
  476. constp.value_set:=constset;
  477. result:=constp;
  478. hp:=tarrayconstructornode(p);
  479. if assigned(hp.left) then
  480. begin
  481. first:=true;
  482. while assigned(hp) do
  483. begin
  484. p4:=nil; { will contain the tree to create the set }
  485. { split a range into p2 and p3 }
  486. if hp.left.nodetype=arrayconstructorrangen then
  487. begin
  488. p2:=tarrayconstructorrangenode(hp.left).left;
  489. p3:=tarrayconstructorrangenode(hp.left).right;
  490. tarrayconstructorrangenode(hp.left).left:=nil;
  491. tarrayconstructorrangenode(hp.left).right:=nil;
  492. end
  493. else
  494. begin
  495. p2:=hp.left;
  496. hp.left:=nil;
  497. p3:=nil;
  498. end;
  499. typecheckpass(p2);
  500. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  501. if assigned(p3) then
  502. begin
  503. typecheckpass(p3);
  504. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  505. end;
  506. if codegenerror then
  507. break;
  508. oldfilepos:=current_filepos;
  509. current_filepos:=p2.fileinfo;
  510. case p2.resultdef.typ of
  511. enumdef,
  512. orddef:
  513. begin
  514. { widechars are not yet supported }
  515. if is_widechar(p2.resultdef) then
  516. begin
  517. if block_type<>bt_const then
  518. inserttypeconv(p2,cansichartype);
  519. if (p2.nodetype<>ordconstn) and not (m_default_unicodestring in current_settings.modeswitches) then
  520. incompatibletypes(cwidechartype,cansichartype);
  521. end;
  522. getrange(p2.resultdef,lr,hr);
  523. if assigned(p3) then
  524. begin
  525. if is_widechar(p3.resultdef) then
  526. begin
  527. if block_type<>bt_const then
  528. inserttypeconv(p3,cansichartype);
  529. if (p3.nodetype<>ordconstn) and not (m_default_unicodestring in current_settings.modeswitches) then
  530. begin
  531. current_filepos:=p3.fileinfo;
  532. incompatibletypes(cwidechartype,cansichartype);
  533. end;
  534. end;
  535. { this isn't good, you'll get problems with
  536. type t010 = 0..10;
  537. ts = set of t010;
  538. var s : ts;b : t010
  539. begin s:=[1,2,b]; end.
  540. if is_integer(p3^.resultdef) then
  541. begin
  542. inserttypeconv(p3,u8bitdef);
  543. end;
  544. }
  545. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  546. begin
  547. CGMessagePos(p3.fileinfo,type_e_typeconflict_in_set);
  548. end
  549. else
  550. begin
  551. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  552. begin
  553. if not(is_integer(p3.resultdef)) then
  554. begin
  555. if not(assigned(hdef)) and first then
  556. hdef:=p3.resultdef;
  557. end
  558. else
  559. begin
  560. inserttypeconv(p3,u8inttype);
  561. inserttypeconv(p2,u8inttype);
  562. end;
  563. if tordconstnode(p2).value.svalue>tordconstnode(p3).value.svalue then
  564. CGMessagePos(p2.fileinfo,type_w_empty_constant_range_set);
  565. for l:=tordconstnode(p2).value.svalue to tordconstnode(p3).value.svalue do
  566. do_set(l);
  567. p2.free;
  568. p2 := nil;
  569. p3.free;
  570. p3 := nil;
  571. end
  572. else
  573. begin
  574. update_constsethi(p2.resultdef,false);
  575. inserttypeconv(p2,hdef);
  576. update_constsethi(p3.resultdef,false);
  577. inserttypeconv(p3,hdef);
  578. if assigned(hdef) then
  579. inserttypeconv(p3,hdef)
  580. else if first then
  581. hdef:=p3.resultdef
  582. else
  583. inserttypeconv(p3,u8inttype);
  584. p4:=csetelementnode.create(p2,p3);
  585. end;
  586. end;
  587. end
  588. else
  589. begin
  590. { Single value }
  591. if p2.nodetype=ordconstn then
  592. begin
  593. if assigned(hdef) then
  594. inserttypeconv(p2,hdef)
  595. else if not(is_integer(p2.resultdef)) and first then
  596. hdef:=p2.resultdef
  597. else
  598. inserttypeconv(p2,u8inttype);
  599. if not(is_integer(p2.resultdef)) then
  600. update_constsethi(p2.resultdef,true);
  601. do_set(tordconstnode(p2).value.svalue);
  602. p2.free;
  603. p2 := nil;
  604. end
  605. else
  606. begin
  607. update_constsethi(p2.resultdef,false);
  608. if assigned(hdef) then
  609. inserttypeconv(p2,hdef)
  610. else if not(is_integer(p2.resultdef)) and first then
  611. hdef:=p2.resultdef
  612. else
  613. inserttypeconv(p2,u8inttype);
  614. p4:=csetelementnode.create(p2,nil);
  615. end;
  616. end;
  617. end;
  618. else
  619. CGMessage(type_e_ordinal_expr_expected);
  620. end;
  621. { insert the set creation tree }
  622. if assigned(p4) then
  623. result:=caddnode.create(addn,result,p4);
  624. { load next and dispose current node }
  625. p2:=hp;
  626. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  627. tarrayconstructornode(p2).right:=nil;
  628. if freep then
  629. p2.free; // no nil needed
  630. current_filepos:=oldfilepos;
  631. first:=false;
  632. end;
  633. if (hdef=nil) then
  634. hdef:=u8inttype;
  635. end
  636. else
  637. begin
  638. { empty set [], only remove node }
  639. if freep then
  640. p.free; // no nil needed
  641. end;
  642. { set the initial set type }
  643. constp.resultdef:=csetdef.create(hdef,constsetlo.svalue,constsethi.svalue,true);
  644. { determine the resultdef for the tree }
  645. typecheckpass(result);
  646. end;
  647. function arrayconstructor_can_be_set(p:tnode):boolean;
  648. var
  649. p1,p2 : tnode;
  650. hdef : tdef;
  651. begin
  652. { keep in sync with arrayconstructor_to_set }
  653. if not assigned(p) then
  654. internalerror(2015050401);
  655. if not assigned(tarrayconstructornode(p).left) then
  656. begin
  657. if assigned(tarrayconstructornode(p).right) then
  658. internalerror(2015050103);
  659. result:=true;
  660. end
  661. else
  662. begin
  663. result:=false;
  664. hdef:=nil;
  665. while assigned(p) do
  666. begin
  667. if tarrayconstructornode(p).left.nodetype=arrayconstructorrangen then
  668. begin
  669. p1:=tarrayconstructorrangenode(tarrayconstructornode(p).left).left;
  670. p2:=tarrayconstructorrangenode(tarrayconstructornode(p).left).right;
  671. end
  672. else
  673. begin
  674. p1:=tarrayconstructornode(p).left;
  675. p2:=nil;
  676. end;
  677. case p1.resultdef.typ of
  678. orddef,
  679. enumdef:
  680. begin
  681. if is_widechar(p1.resultdef) then
  682. begin
  683. if p1.nodetype<>ordconstn then
  684. exit
  685. else if (tordconstnode(p1).value.uvalue>high(byte)) and not (m_default_unicodestring in current_settings.modeswitches) then
  686. exit;
  687. end;
  688. if assigned(p2) then
  689. begin
  690. if is_widechar(p2.resultdef) then
  691. begin
  692. if p2.nodetype<>ordconstn then
  693. exit
  694. else if (tordconstnode(p2).value.uvalue>high(byte)) and not (m_default_unicodestring in current_settings.modeswitches) then
  695. exit;
  696. end;
  697. { anything to exclude? }
  698. end
  699. else
  700. begin
  701. { anything to exclude? }
  702. end;
  703. end;
  704. stringdef:
  705. if p1.nodetype<>stringconstn then
  706. exit
  707. else if assigned(hdef) and not is_char(hdef) then
  708. exit;
  709. else
  710. exit;
  711. end;
  712. p:=tarrayconstructornode(p).right;
  713. end;
  714. result:=true;
  715. end;
  716. end;
  717. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  718. begin
  719. { procvars without arguments in variant arrays are always called by
  720. Delphi }
  721. if not(iscvarargs) then
  722. maybe_call_procvar(p,true);
  723. if not(iscvarargs) and
  724. (p.nodetype=stringconstn) and
  725. { don't cast to AnsiString if already casted to Wide/UnicodeString, issue #18266 }
  726. (tstringconstnode(p).cst_type in [cst_conststring,cst_shortstring,cst_longstring]) then
  727. p:=ctypeconvnode.create_internal(p,getansistringdef)
  728. else
  729. case p.resultdef.typ of
  730. enumdef :
  731. p:=ctypeconvnode.create_internal(p,s32inttype);
  732. arraydef :
  733. begin
  734. if is_chararray(p.resultdef) then
  735. p:=ctypeconvnode.create_internal(p,charpointertype)
  736. else
  737. if is_widechararray(p.resultdef) then
  738. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  739. else
  740. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  741. end;
  742. orddef :
  743. begin
  744. if is_integer(p.resultdef) and
  745. not(is_64bitint(p.resultdef)) then
  746. if not(m_delphi in current_settings.modeswitches) then
  747. p:=ctypeconvnode.create(p,s32inttype)
  748. else
  749. { delphi doesn't generate a range error when passing a
  750. cardinal >= $80000000, but since these are seen as
  751. longint on the callee side, this causes data loss;
  752. as a result, we require an explicit longint()
  753. typecast in FPC mode on the caller side if range
  754. checking should be disabled, but not in Delphi mode }
  755. p:=ctypeconvnode.create_internal(p,s32inttype)
  756. else if is_void(p.resultdef) then
  757. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  758. else if iscvarargs and is_currency(p.resultdef)
  759. and (current_settings.fputype<>fpu_none) then
  760. p:=ctypeconvnode.create(p,s64floattype);
  761. end;
  762. floatdef :
  763. if not(iscvarargs) then
  764. begin
  765. if not(is_currency(p.resultdef)) then
  766. p:=ctypeconvnode.create(p,pbestrealtype^);
  767. end
  768. else
  769. begin
  770. if is_constrealnode(p) and
  771. not(nf_explicit in p.flags) then
  772. MessagePos(p.fileinfo,type_w_double_c_varargs);
  773. if (tfloatdef(p.resultdef).floattype in [s32real,s64currency]) or
  774. (is_constrealnode(p) and
  775. not(nf_explicit in p.flags)) then
  776. p:=ctypeconvnode.create(p,s64floattype);
  777. end;
  778. procvardef :
  779. p:=ctypeconvnode.create(p,voidpointertype);
  780. stringdef:
  781. if iscvarargs then
  782. p:=ctypeconvnode.create(p,charpointertype);
  783. variantdef:
  784. if iscvarargs then
  785. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  786. { maybe warn in case it's not using "packrecords c"? }
  787. recorddef:
  788. if not iscvarargs then
  789. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  790. pointerdef:
  791. ;
  792. classrefdef:
  793. if iscvarargs then
  794. p:=ctypeconvnode.create(p,voidpointertype);
  795. objectdef :
  796. if is_objc_class_or_protocol(p.resultdef) then
  797. p:=ctypeconvnode.create(p,voidpointertype)
  798. else if iscvarargs or
  799. is_object(p.resultdef) then
  800. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  801. else
  802. else
  803. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  804. end;
  805. typecheckpass(p);
  806. end;
  807. { in FPC mode, @procname immediately has to be evaluated as a
  808. procvar. If procname is global, then this will be a global
  809. procvar. Since converting global procvars to local procvars is
  810. not allowed (see point d in defcmp.proc_to_procvar_equal()),
  811. this results in errors when passing global procedures to local
  812. procvar parameters or assigning them to nested procvars. The
  813. solution is to remove the (wrong) conversion to a global procvar,
  814. and instead insert a conversion to the local procvar type. }
  815. function maybe_global_proc_to_nested(var fromnode: tnode; todef: tdef): boolean;
  816. var
  817. hp: tnode;
  818. begin
  819. result:=false;
  820. if (m_nested_procvars in current_settings.modeswitches) and
  821. not(m_tp_procvar in current_settings.modeswitches) and
  822. (todef.typ=procvardef) and
  823. is_nested_pd(tprocvardef(todef)) and
  824. (fromnode.nodetype=typeconvn) and
  825. (ttypeconvnode(fromnode).convtype=tc_proc_2_procvar) and
  826. not is_nested_pd(tprocvardef(fromnode.resultdef)) and
  827. (proc_to_procvar_equal(tprocdef(ttypeconvnode(fromnode).left.resultdef),tprocvardef(todef),false)>=te_convert_l1) then
  828. begin
  829. hp:=fromnode;
  830. fromnode:=ctypeconvnode.create_proc_to_procvar(ttypeconvnode(fromnode).left);
  831. ttypeconvnode(fromnode).totypedef:=todef;
  832. typecheckpass(fromnode);
  833. ttypeconvnode(hp).left:=nil;
  834. hp.free;
  835. hp := nil;
  836. result:=true;
  837. end;
  838. end;
  839. { similar as above, but for assigning @classtype.method to a
  840. procvar of object. pexpr.do_proc_call() stores the symtable of classtype
  841. in the loadnode so we can retrieve it here (rather than the symtable in
  842. which method was found, which may be a parent class) }
  843. function maybe_classmethod_to_methodprocvar(var fromnode: tnode; todef: tdef): boolean;
  844. var
  845. hp: tnode;
  846. begin
  847. result:=false;
  848. if not(m_tp_procvar in current_settings.modeswitches) and
  849. (todef.typ=procvardef) and
  850. is_methodpointer(tprocvardef(todef)) and
  851. (fromnode.nodetype=typeconvn) and
  852. (ttypeconvnode(fromnode).convtype=tc_proc_2_procvar) and
  853. is_methodpointer(fromnode.resultdef) and
  854. (po_classmethod in tprocvardef(fromnode.resultdef).procoptions) and
  855. not(po_staticmethod in tprocvardef(fromnode.resultdef).procoptions) and
  856. (proc_to_procvar_equal(tprocdef(ttypeconvnode(fromnode).left.resultdef),tprocvardef(todef),false)>=te_convert_l1) then
  857. begin
  858. hp:=fromnode;
  859. fromnode:=ttypeconvnode(fromnode).left;
  860. if (fromnode.nodetype=loadn) and
  861. not assigned(tloadnode(fromnode).left) then
  862. tloadnode(fromnode).set_mp(cloadvmtaddrnode.create(ctypenode.create(tdef(tloadnode(fromnode).symtable.defowner))));
  863. fromnode:=ctypeconvnode.create_proc_to_procvar(fromnode);
  864. ttypeconvnode(fromnode).totypedef:=todef;
  865. typecheckpass(fromnode);
  866. ttypeconvnode(hp).left:=nil;
  867. hp.free;
  868. hp := nil;
  869. result:=true;
  870. end;
  871. end;
  872. {*****************************************************************************
  873. TTYPECONVNODE
  874. *****************************************************************************}
  875. constructor ttypeconvnode.create(node : tnode;def:tdef);
  876. begin
  877. inherited create(typeconvn,node);
  878. convtype:=tc_none;
  879. convnodeflags:=[];
  880. totypedef:=def;
  881. if def=nil then
  882. internalerror(200103281);
  883. fileinfo:=node.fileinfo;
  884. {An attempt to convert the result of a floating point division
  885. (with the / operator) to an integer type will fail. Give a hint
  886. to use the div operator.}
  887. if (node.nodetype=slashn) and (def.typ=orddef) and not(is_currency(def)) then
  888. cgmessage(type_h_use_div_for_int);
  889. {In expressions like int64:=longint+longint, an integer overflow could be avoided
  890. by simply converting the operands to int64 first. Give a hint to do this.}
  891. if (node.nodetype in [addn,subn,muln]) and
  892. (def.typ=orddef) and (node.resultdef<>nil) and (node.resultdef.typ=orddef) and
  893. ((Torddef(node.resultdef).low>=Torddef(def).low) and (Torddef(node.resultdef).high<=Torddef(def).high)) and
  894. ((Torddef(node.resultdef).low>Torddef(def).low) or (Torddef(node.resultdef).high<Torddef(def).high)) then
  895. case node.nodetype of
  896. addn:
  897. cgmessage1(type_h_convert_add_operands_to_prevent_overflow,def.typename);
  898. subn:
  899. cgmessage1(type_h_convert_sub_operands_to_prevent_overflow,def.typename);
  900. muln:
  901. cgmessage1(type_h_convert_mul_operands_to_prevent_overflow,def.typename);
  902. else
  903. ;
  904. end;
  905. end;
  906. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  907. begin
  908. self.create(node,def);
  909. include(flags,nf_explicit);
  910. end;
  911. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  912. begin
  913. self.create(node,def);
  914. { handle like explicit conversions }
  915. include(flags,nf_explicit);
  916. include(flags,nf_internal);
  917. end;
  918. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  919. begin
  920. self.create(node,voidtype);
  921. convtype:=tc_proc_2_procvar;
  922. end;
  923. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  924. begin
  925. inherited ppuload(t,ppufile);
  926. ppufile.getderef(totypedefderef);
  927. convtype:=tconverttype(ppufile.getbyte);
  928. ppufile.getset(tppuset1(convnodeflags));
  929. end;
  930. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  931. begin
  932. inherited ppuwrite(ppufile);
  933. ppufile.putderef(totypedefderef);
  934. ppufile.putbyte(byte(convtype));
  935. ppufile.putset(tppuset1(convnodeflags));
  936. end;
  937. procedure ttypeconvnode.buildderefimpl;
  938. begin
  939. inherited buildderefimpl;
  940. totypedefderef.build(totypedef);
  941. end;
  942. procedure ttypeconvnode.derefimpl;
  943. begin
  944. inherited derefimpl;
  945. totypedef:=tdef(totypedefderef.resolve);
  946. end;
  947. function ttypeconvnode.dogetcopy : tnode;
  948. var
  949. n : ttypeconvnode;
  950. begin
  951. n:=ttypeconvnode(inherited dogetcopy);
  952. n.convtype:=convtype;
  953. n.convnodeflags:=convnodeflags;
  954. n.totypedef:=totypedef;
  955. n.assignment_side:=assignment_side;
  956. dogetcopy:=n;
  957. end;
  958. procedure ttypeconvnode.printnodeinfo(var t : text);
  959. var
  960. first: Boolean;
  961. i: ttypeconvnodeflag;
  962. begin
  963. inherited printnodeinfo(t);
  964. write(t,', totypedef = ',totypedef.GetTypeName);
  965. write(t,', convtype = ',convtype);
  966. write(t,', assignment_side = ',assignment_side);
  967. write(t,', convnodeflags = [');
  968. first:=true;
  969. for i:=low(ttypeconvnodeflag) to high(ttypeconvnodeflag) do
  970. if i in convnodeflags then
  971. begin
  972. if not first then
  973. write(t,',')
  974. else
  975. first:=false;
  976. write(t,i);
  977. end;
  978. write(t,']');
  979. end;
  980. {$ifdef DEBUG_NODE_XML}
  981. procedure TTypeConvNode.XMLPrintNodeInfo(var T: Text);
  982. var
  983. First: Boolean;
  984. i: TTypeConvNodeFlag;
  985. begin
  986. inherited XMLPrintNodeInfo(T);
  987. Write(T,' convtype="', convtype);
  988. First := True;
  989. for i := Low(TTypeConvNodeFlag) to High(TTypeConvNodeFlag) do
  990. if i in ConvNodeFlags then
  991. begin
  992. if First then
  993. begin
  994. Write(T, '" convnodeflags="', i);
  995. First := False;
  996. end
  997. else
  998. Write(T, ',', i);
  999. end;
  1000. { If no flags were printed, this is the closing " for convtype }
  1001. Write(T, '"');
  1002. end;
  1003. {$endif DEBUG_NODE_XML}
  1004. function ttypeconvnode.typecheck_cord_to_pointer : tnode;
  1005. begin
  1006. result:=nil;
  1007. if left.nodetype=ordconstn then
  1008. begin
  1009. { check if we have a valid pointer constant (JM) }
  1010. {$if sizeof(pointer) > sizeof(TConstPtrUInt)}
  1011. {$if sizeof(TConstPtrUInt) = 4}
  1012. if (tordconstnode(left).value < int64(low(longint))) or
  1013. (tordconstnode(left).value > int64(high(cardinal))) then
  1014. CGMessage(parser_e_range_check_error);
  1015. {$else} {$if sizeof(TConstPtrUInt) = 8}
  1016. if (tordconstnode(left).value < int64(low(int64))) or
  1017. (tordconstnode(left).value > int64(high(qword))) then
  1018. CGMessage(parser_e_range_check_error);
  1019. {$else}
  1020. internalerror(2001020801);
  1021. {$endif} {$endif}
  1022. {$endif}
  1023. if not(nf_explicit in flags) then
  1024. if (tordconstnode(left).value.svalue=0) then
  1025. CGMessage(type_w_zero_to_nil)
  1026. else
  1027. { in Delphi mode, these aren't caught in compare_defs_ext }
  1028. IncompatibleTypes(left.resultdef,resultdef);
  1029. result:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  1030. end
  1031. else
  1032. internalerror(200104023);
  1033. end;
  1034. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  1035. var
  1036. chartype : string[8];
  1037. newblock : tblocknode;
  1038. newstat : tstatementnode;
  1039. restemp : ttempcreatenode;
  1040. begin
  1041. if is_widechar(tarraydef(left.resultdef).elementdef) then
  1042. chartype:='widechar'
  1043. else
  1044. chartype:='char';
  1045. if tstringdef(resultdef).stringtype=st_shortstring then
  1046. begin
  1047. newblock:=internalstatements(newstat);
  1048. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1049. addstatement(newstat,restemp);
  1050. addstatement(newstat,ccallnode.createintern('fpc_'+chartype+'array_to_shortstr',
  1051. ccallparanode.create(cordconstnode.create(
  1052. ord(tarraydef(left.resultdef).lowrange=0),pasbool1type,false),
  1053. ccallparanode.create(left,ccallparanode.create(
  1054. ctemprefnode.create(restemp),nil)))));
  1055. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1056. addstatement(newstat,ctemprefnode.create(restemp));
  1057. result:=newblock;
  1058. end
  1059. else if (tstringdef(resultdef).stringtype=st_ansistring) then
  1060. begin
  1061. result:=ccallnode.createinternres(
  1062. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  1063. ccallparanode.create(
  1064. cordconstnode.create(
  1065. ord(tarraydef(left.resultdef).lowrange=0),
  1066. pasbool1type,
  1067. false
  1068. ),
  1069. ccallparanode.create(
  1070. cordconstnode.create(
  1071. getparaencoding(resultdef),
  1072. u16inttype,
  1073. true
  1074. ),
  1075. ccallparanode.create(left,nil)
  1076. )
  1077. ),
  1078. resultdef
  1079. );
  1080. end
  1081. else
  1082. result:=ccallnode.createinternres(
  1083. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  1084. ccallparanode.create(cordconstnode.create(
  1085. ord(tarraydef(left.resultdef).lowrange=0),pasbool1type,false),
  1086. ccallparanode.create(left,nil)),resultdef);
  1087. left:=nil;
  1088. end;
  1089. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  1090. var
  1091. newblock : tblocknode;
  1092. newstat : tstatementnode;
  1093. restemp : ttempcreatenode;
  1094. pchtemp : pchar;
  1095. arrsize : tcgint;
  1096. chartype : string[8];
  1097. begin
  1098. result := nil;
  1099. with tarraydef(resultdef) do
  1100. begin
  1101. if highrange<lowrange then
  1102. internalerror(2005010502);
  1103. arrsize := highrange-lowrange+1;
  1104. end;
  1105. if (left.nodetype = stringconstn) and
  1106. (tstringconstnode(left).cst_type=cst_conststring) then
  1107. begin
  1108. if (m_iso in current_settings.modeswitches) and (arrsize<>tstringconstnode(left).len) and
  1109. is_char(tarraydef(resultdef).elementdef) then
  1110. Message2(type_w_array_size_does_not_match_size_of_constant_string,tostr(tstringconstnode(left).len),tostr(arrsize));
  1111. { if the array of char is large enough we can use the string
  1112. constant directly. This is handled in ncgcnv }
  1113. if (arrsize>=tstringconstnode(left).len) and
  1114. is_char(tarraydef(resultdef).elementdef) then
  1115. begin
  1116. { pad the constant string with #0 to the array len }
  1117. { (2.0.x compatible) }
  1118. if (arrsize>tstringconstnode(left).len) then
  1119. begin
  1120. pchtemp:=concatansistrings(tstringconstnode(left).asconstpchar,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  1121. left.free;
  1122. left:=cstringconstnode.createpchar(pchtemp,arrsize,nil);
  1123. freemem(pchtemp);
  1124. typecheckpass(left);
  1125. end;
  1126. exit;
  1127. end;
  1128. { Convert to wide/short/ansistring and call default helper }
  1129. if is_widechar(tarraydef(resultdef).elementdef) then
  1130. inserttypeconv(left,cunicodestringtype)
  1131. else
  1132. begin
  1133. if tstringconstnode(left).len>255 then
  1134. inserttypeconv(left,getansistringdef)
  1135. else
  1136. inserttypeconv(left,cshortstringtype);
  1137. end;
  1138. end;
  1139. if is_widechar(tarraydef(resultdef).elementdef) then
  1140. chartype:='widechar'
  1141. else
  1142. chartype:='char';
  1143. newblock:=internalstatements(newstat);
  1144. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1145. addstatement(newstat,restemp);
  1146. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  1147. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  1148. ctemprefnode.create(restemp),nil))));
  1149. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1150. addstatement(newstat,ctemprefnode.create(restemp));
  1151. result:=newblock;
  1152. left:=nil;
  1153. end;
  1154. function ttypeconvnode.typecheck_char_to_string : tnode;
  1155. var
  1156. procname: string[31];
  1157. para : tcallparanode;
  1158. hp : tstringconstnode;
  1159. ws : tcompilerwidestring;
  1160. sa : ansistring;
  1161. cw : tcompilerwidechar;
  1162. l : SizeUInt;
  1163. exprtype : tdef;
  1164. begin
  1165. result:=nil;
  1166. sa:='';
  1167. if (left.nodetype=ordconstn) and
  1168. ((tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring]) or
  1169. (torddef(left.resultdef).ordtype in [uchar,uwidechar])) then
  1170. begin
  1171. if (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
  1172. begin
  1173. initwidestring(ws);
  1174. if torddef(left.resultdef).ordtype=uwidechar then
  1175. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  1176. else
  1177. concatwidestringchar(ws,asciichar2unicode(chr(tordconstnode(left).value.uvalue)));
  1178. hp:=cstringconstnode.createunistr(ws);
  1179. hp.changestringtype(resultdef);
  1180. donewidestring(ws);
  1181. end
  1182. else
  1183. begin
  1184. if (torddef(left.resultdef).ordtype=uwidechar) then
  1185. begin
  1186. if not((current_settings.sourcecodepage=CP_UTF8) or
  1187. ((tstringdef(resultdef).stringtype=st_ansistring) and
  1188. (tstringdef(resultdef).encoding=CP_UTF8))) then
  1189. begin
  1190. if tordconstnode(left).value.uvalue>127 then
  1191. begin
  1192. Message(type_w_unicode_data_loss);
  1193. // compiler has different codepage than a system running an application
  1194. // to prevent wrong codepage and data loss we are converting unicode char
  1195. // using a helper routine. This is not delphi compatible behavior.
  1196. // Delphi converts UniocodeChar to ansistring at the compile time
  1197. // old behavior:
  1198. // hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)));
  1199. para:=ccallparanode.create(left,nil);
  1200. if tstringdef(resultdef).stringtype=st_ansistring then
  1201. para:=ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),para);
  1202. result:=ccallnode.createinternres('fpc_uchar_to_'+tstringdef(resultdef).stringtypname,
  1203. para,resultdef);
  1204. left:=nil;
  1205. exit;
  1206. end
  1207. else
  1208. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)));
  1209. end
  1210. else
  1211. begin
  1212. cw:=tcompilerwidechar(tordconstnode(left).value.uvalue);
  1213. SetLength(sa,5);
  1214. l:=UnicodeToUtf8(@(sa[1]),Length(sa),@cw,1);
  1215. SetLength(sa,l-1);
  1216. hp:=cstringconstnode.createstr(sa);
  1217. { explicitly set the type of string constant to avoid unnecessary conversion }
  1218. if (tstringdef(resultdef).stringtype=st_ansistring) and
  1219. (tstringdef(resultdef).encoding=CP_UTF8) then
  1220. begin
  1221. hp.cst_type:=cst_ansistring;
  1222. hp.resultdef:=resultdef;
  1223. end;
  1224. end
  1225. end
  1226. else
  1227. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  1228. { output string consts in local ansistring encoding }
  1229. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0) or (tstringdef(resultdef).encoding=globals.CP_NONE)) then
  1230. tstringconstnode(hp).changestringtype(getansistringdef)
  1231. else
  1232. tstringconstnode(hp).changestringtype(resultdef);
  1233. end;
  1234. result:=hp;
  1235. end
  1236. else
  1237. { shortstrings are handled 'inline' (except for widechars) }
  1238. if (tstringdef(resultdef).stringtype<>st_shortstring) or
  1239. (torddef(left.resultdef).ordtype=uwidechar) or
  1240. (target_info.system in systems_managed_vm) then
  1241. begin
  1242. { parameter }
  1243. para:=ccallparanode.create(left,nil);
  1244. { encoding required? }
  1245. if tstringdef(resultdef).stringtype=st_ansistring then
  1246. para:=ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),para);
  1247. { create the procname }
  1248. if torddef(left.resultdef).ordtype<>uwidechar then
  1249. begin
  1250. procname:='fpc_char_to_';
  1251. if tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring] then
  1252. if nf_explicit in flags then
  1253. Message2(type_w_explicit_string_cast,left.resultdef.typename,resultdef.typename)
  1254. else
  1255. Message2(type_w_implicit_string_cast,left.resultdef.typename,resultdef.typename);
  1256. end
  1257. else
  1258. begin
  1259. procname:='fpc_uchar_to_';
  1260. if not (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
  1261. if nf_explicit in flags then
  1262. Message2(type_w_explicit_string_cast_loss,left.resultdef.typename,resultdef.typename)
  1263. else
  1264. Message2(type_w_implicit_string_cast_loss,left.resultdef.typename,resultdef.typename);
  1265. end;
  1266. procname:=procname+tstringdef(resultdef).stringtypname;
  1267. { and finally the call }
  1268. result:=ccallnode.createinternres(procname,para,resultdef);
  1269. left := nil;
  1270. end
  1271. else
  1272. begin
  1273. { use at least u16inttype }
  1274. {$ifdef cpu8bitalu}
  1275. exprtype:=u16inttype;
  1276. {$else cpu8bitalu}
  1277. exprtype:=uinttype;
  1278. {$endif cpu8bitalu}
  1279. { create word(byte(char) shl 8 or 1) for litte endian machines }
  1280. { and word(byte(char) or 256) for big endian machines }
  1281. left := ctypeconvnode.create_internal(left,exprtype);
  1282. if (target_info.endian = endian_little) then
  1283. left := caddnode.create(orn,
  1284. cshlshrnode.create(shln,left,cordconstnode.create(8,exprtype,false)),
  1285. cordconstnode.create(1,exprtype,false))
  1286. else
  1287. left := caddnode.create(orn,left,
  1288. cordconstnode.create(1 shl 8,exprtype,false));
  1289. left := ctypeconvnode.create_internal(left,u16inttype);
  1290. typecheckpass(left);
  1291. end;
  1292. end;
  1293. function ttypeconvnode.typecheck_string_to_string : tnode;
  1294. begin
  1295. result:=nil;
  1296. if (left.nodetype=stringconstn) and
  1297. (((tstringdef(resultdef).stringtype=st_ansistring) and
  1298. (tstringdef(resultdef).encoding<>CP_NONE)
  1299. )
  1300. ) and
  1301. (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1302. begin
  1303. tstringconstnode(left).changestringtype(resultdef);
  1304. Result:=left;
  1305. left:=nil;
  1306. end
  1307. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  1308. (tstringdef(left.resultdef).stringtype=st_ansistring) and
  1309. (tstringdef(resultdef).encoding<>tstringdef(left.resultdef).encoding) then
  1310. begin
  1311. result:=ccallnode.createinternres(
  1312. 'fpc_ansistr_to_ansistr',
  1313. ccallparanode.create(
  1314. cordconstnode.create(
  1315. tstringdef(resultdef).encoding,
  1316. u16inttype,
  1317. true
  1318. ),
  1319. ccallparanode.create(left,nil)
  1320. ),
  1321. resultdef
  1322. );
  1323. left:=nil;
  1324. end
  1325. else if (left.nodetype=stringconstn) and
  1326. (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1327. (tstringdef(resultdef).stringtype=st_shortstring) then
  1328. begin
  1329. if not hasnonasciichars(tstringconstnode(left).valuews) then
  1330. begin
  1331. tstringconstnode(left).changestringtype(resultdef);
  1332. Result:=left;
  1333. left:=nil;
  1334. end;
  1335. end
  1336. else if (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1337. not (tstringdef(resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1338. begin
  1339. if nf_explicit in flags then
  1340. Message2(type_w_explicit_string_cast_loss,left.resultdef.typename,resultdef.typename)
  1341. else
  1342. Message2(type_w_implicit_string_cast_loss,left.resultdef.typename,resultdef.typename);
  1343. end
  1344. else if not (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1345. (tstringdef(resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1346. begin
  1347. if nf_explicit in flags then
  1348. Message2(type_w_explicit_string_cast,left.resultdef.typename,resultdef.typename)
  1349. else
  1350. Message2(type_w_implicit_string_cast,left.resultdef.typename,resultdef.typename);
  1351. end
  1352. end;
  1353. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  1354. begin
  1355. result:=nil;
  1356. end;
  1357. function ttypeconvnode.typecheck_char_to_char : tnode;
  1358. var
  1359. hp : tordconstnode;
  1360. begin
  1361. result:=nil;
  1362. if (left.nodetype=ordconstn) and
  1363. ((torddef(resultdef).ordtype<>uchar) or
  1364. (torddef(left.resultdef).ordtype<>uwidechar) or
  1365. (current_settings.sourcecodepage<>CP_UTF8))
  1366. then
  1367. begin
  1368. if (torddef(resultdef).ordtype=uchar) and
  1369. (torddef(left.resultdef).ordtype=uwidechar) and
  1370. (current_settings.sourcecodepage<>CP_UTF8) then
  1371. begin
  1372. if tordconstnode(left).value.uvalue>127 then
  1373. Message(type_w_unicode_data_loss);
  1374. hp:=cordconstnode.create(
  1375. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  1376. cansichartype,true);
  1377. result:=hp;
  1378. end
  1379. else if (torddef(resultdef).ordtype=uwidechar) and
  1380. (torddef(left.resultdef).ordtype=uchar) then
  1381. begin
  1382. hp:=cordconstnode.create(
  1383. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  1384. cwidechartype,true);
  1385. result:=hp;
  1386. end
  1387. else
  1388. internalerror(200105131);
  1389. exit;
  1390. end;
  1391. end;
  1392. function ttypeconvnode.typecheck_int_to_int : tnode;
  1393. var
  1394. v : TConstExprInt;
  1395. begin
  1396. result:=nil;
  1397. if left.nodetype=ordconstn then
  1398. begin
  1399. v:=tordconstnode(left).value;
  1400. if is_currency(resultdef) and
  1401. not(nf_internal in flags) then
  1402. v:=v*10000;
  1403. if (resultdef.typ=pointerdef) then
  1404. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1405. else
  1406. begin
  1407. if is_currency(left.resultdef) then
  1408. begin
  1409. if not(nf_internal in flags) then
  1410. v:=v div 10000;
  1411. end
  1412. else if (resultdef.typ in [orddef,enumdef]) then
  1413. adaptrange(resultdef,v,([nf_internal,nf_absolute]*flags)<>[],nf_explicit in flags,cs_check_range in localswitches);
  1414. result:=cordconstnode.create(v,resultdef,false);
  1415. end;
  1416. end
  1417. else if left.nodetype=pointerconstn then
  1418. begin
  1419. v:=tpointerconstnode(left).value;
  1420. if (resultdef.typ=pointerdef) then
  1421. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1422. else
  1423. begin
  1424. if is_currency(resultdef) and
  1425. not(nf_internal in flags) then
  1426. v:=v*10000;
  1427. result:=cordconstnode.create(v,resultdef,false);
  1428. end;
  1429. end
  1430. else
  1431. begin
  1432. if (is_currency(resultdef) or
  1433. is_currency(left.resultdef)) and
  1434. (nf_internal in flags) then
  1435. begin
  1436. include(flags,nf_is_currency)
  1437. end
  1438. { multiply by 10000 for currency. We need to use getcopy to pass
  1439. the argument because the current node is always disposed. Only
  1440. inserting the multiply in the left node is not possible because
  1441. it'll get in an infinite loop to convert int->currency }
  1442. else if is_currency(resultdef) then
  1443. begin
  1444. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1445. include(result.flags,nf_is_currency);
  1446. include(taddnode(result).left.flags,nf_internal);
  1447. end
  1448. else if is_currency(left.resultdef) then
  1449. begin
  1450. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1451. include(result.flags,nf_is_currency);
  1452. include(tmoddivnode(result).left.flags,nf_internal);
  1453. end;
  1454. end;
  1455. end;
  1456. function ttypeconvnode.typecheck_int_to_real : tnode;
  1457. var
  1458. rv : bestreal;
  1459. begin
  1460. result:=nil;
  1461. if left.nodetype=ordconstn then
  1462. begin
  1463. rv:=tordconstnode(left).value;
  1464. if is_currency(resultdef) and
  1465. not(nf_internal in flags) then
  1466. rv:=rv*10000.0
  1467. else if is_currency(left.resultdef) and
  1468. not(nf_internal in flags) then
  1469. rv:=rv/10000.0;
  1470. result:=crealconstnode.create(rv,resultdef);
  1471. end
  1472. else
  1473. begin
  1474. if (is_currency(resultdef) or
  1475. is_currency(left.resultdef)) and
  1476. (nf_internal in flags) then
  1477. begin
  1478. include(flags,nf_is_currency)
  1479. end
  1480. { multiply by 10000 for currency. We need to use getcopy to pass
  1481. the argument because the current node is always disposed. Only
  1482. inserting the multiply in the left node is not possible because
  1483. it'll get in an infinite loop to convert int->currency }
  1484. else if is_currency(resultdef) then
  1485. begin
  1486. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1487. include(result.flags,nf_is_currency);
  1488. end
  1489. else if is_currency(left.resultdef) then
  1490. begin
  1491. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1492. include(result.flags,nf_is_currency);
  1493. end;
  1494. end;
  1495. end;
  1496. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1497. begin
  1498. if not is_currency(resultdef) then
  1499. internalerror(200304221);
  1500. result:=nil;
  1501. if not(nf_internal in flags) then
  1502. begin
  1503. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1504. include(left.flags,nf_is_currency);
  1505. { Convert constants directly, else call Round() }
  1506. if left.nodetype=realconstn then
  1507. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1508. else
  1509. begin
  1510. result:=cinlinenode.create(in_round_real,false,left);
  1511. { Internal type cast to currency }
  1512. result:=ctypeconvnode.create_internal(result,s64currencytype);
  1513. left:=nil;
  1514. end
  1515. end
  1516. else
  1517. begin
  1518. include(left.flags,nf_is_currency);
  1519. result:=left;
  1520. left:=nil;
  1521. end;
  1522. end;
  1523. function ttypeconvnode.typecheck_real_to_real : tnode;
  1524. begin
  1525. result:=nil;
  1526. if not(nf_internal in flags) then
  1527. begin
  1528. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1529. begin
  1530. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1531. include(left.flags,nf_is_currency);
  1532. typecheckpass(left);
  1533. end
  1534. else
  1535. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1536. begin
  1537. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1538. include(left.flags,nf_is_currency);
  1539. include(flags,nf_is_currency);
  1540. typecheckpass(left);
  1541. end;
  1542. { comp is handled by the fpu but not a floating type point }
  1543. if is_fpucomp(resultdef) and not(is_fpucomp(left.resultdef)) and
  1544. not (nf_explicit in flags) then
  1545. Message(type_w_convert_real_2_comp);
  1546. end
  1547. else
  1548. include(flags,nf_is_currency);
  1549. end;
  1550. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1551. begin
  1552. result:=nil;
  1553. { handle any constants via cunicodestringtype because the compiler
  1554. cannot convert arbitrary unicodechar constants at compile time to
  1555. a shortstring (since it doesn't know the code page to use) }
  1556. inserttypeconv(left,cunicodestringtype);
  1557. { evaluate again, reset resultdef so the convert_typ
  1558. will be calculated again and cstring_to_pchar will
  1559. be used for futher conversion }
  1560. convtype:=tc_none;
  1561. result:=pass_typecheck;
  1562. end;
  1563. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1564. begin
  1565. result:=nil;
  1566. if is_pwidechar(resultdef) then
  1567. inserttypeconv(left,cunicodestringtype)
  1568. else
  1569. if is_pchar(resultdef) and
  1570. (is_widestring(left.resultdef) or
  1571. is_unicodestring(left.resultdef)) then
  1572. begin
  1573. inserttypeconv(left,getansistringdef);
  1574. { the second pass of second_cstring_to_pchar expects a }
  1575. { strinconstn, but this may become a call to the }
  1576. { widestring manager in case left contains "high ascii" }
  1577. if (left.nodetype<>stringconstn) then
  1578. begin
  1579. result:=left;
  1580. left:=nil;
  1581. end;
  1582. end;
  1583. end;
  1584. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1585. var
  1586. fcc : cardinal;
  1587. pb : pbyte;
  1588. begin
  1589. result:=nil;
  1590. if left.nodetype<>stringconstn then
  1591. internalerror(200510012);
  1592. if (m_mac in current_settings.modeswitches) and
  1593. is_integer(resultdef) and
  1594. (tstringconstnode(left).cst_type=cst_conststring) and
  1595. (tstringconstnode(left).len=4) then
  1596. begin
  1597. pb:=pbyte(tstringconstnode(left).asconstpchar);
  1598. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1599. result:=cordconstnode.create(fcc,u32inttype,false);
  1600. end
  1601. else
  1602. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1603. end;
  1604. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1605. var
  1606. hp : tnode;
  1607. begin
  1608. result:=nil;
  1609. if left.nodetype<>arrayconstructorn then
  1610. internalerror(5546);
  1611. { remove typeconv node }
  1612. hp:=left;
  1613. left:=nil;
  1614. { create a set constructor tree }
  1615. arrayconstructor_to_set(hp);
  1616. if is_emptyset(hp) then
  1617. begin
  1618. { enforce the result type for an empty set }
  1619. hp.resultdef:=resultdef;
  1620. result:=hp;
  1621. end
  1622. else if hp.resultdef<>resultdef then
  1623. begin
  1624. { the set might contain a subrange element (e.g. through a variable),
  1625. thus we need to insert another type conversion }
  1626. if nf_explicit in flags then
  1627. result:=ctypeconvnode.create_explicit(hp,totypedef)
  1628. else if nf_internal in flags then
  1629. result:=ctypeconvnode.create_internal(hp,totypedef)
  1630. else
  1631. result:=ctypeconvnode.create(hp,totypedef);
  1632. end
  1633. else
  1634. result:=hp;
  1635. end;
  1636. function ttypeconvnode.typecheck_set_to_set : tnode;
  1637. begin
  1638. result:=nil;
  1639. { constant sets can be converted by changing the type only }
  1640. if (left.nodetype=setconstn) then
  1641. begin
  1642. if (cs_check_range in current_settings.localswitches) and (tsetconstnode(left).elements>0) and ((tsetconstnode(left).low<tsetdef(resultdef).setlow) or (tsetconstnode(left).high>tsetdef(resultdef).setmax)) then
  1643. Message(parser_e_range_check_error);
  1644. left.resultdef:=resultdef;
  1645. result:=left;
  1646. left:=nil;
  1647. exit;
  1648. end;
  1649. end;
  1650. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1651. var
  1652. newblock : tblocknode;
  1653. newstat : tstatementnode;
  1654. restemp : ttempcreatenode;
  1655. begin
  1656. if tstringdef(resultdef).stringtype=st_shortstring then
  1657. begin
  1658. newblock:=internalstatements(newstat);
  1659. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1660. addstatement(newstat,restemp);
  1661. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1662. ctemprefnode.create(restemp),nil))));
  1663. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1664. addstatement(newstat,ctemprefnode.create(restemp));
  1665. result:=newblock;
  1666. end
  1667. else if tstringdef(resultdef).stringtype=st_ansistring then
  1668. result := ccallnode.createinternres(
  1669. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1670. ccallparanode.create(
  1671. cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  1672. ccallparanode.create(left,nil)
  1673. ),
  1674. resultdef
  1675. )
  1676. else
  1677. result := ccallnode.createinternres(
  1678. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1679. ccallparanode.create(left,nil),resultdef);
  1680. left:=nil;
  1681. end;
  1682. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1683. begin
  1684. if assigned(tobjectdef(left.resultdef).iidstr) then
  1685. begin
  1686. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1687. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1688. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1689. tstringconstnode(result).changestringtype(cshortstringtype);
  1690. end
  1691. else
  1692. internalerror(2013112913);
  1693. end;
  1694. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1695. begin
  1696. if assigned(tobjectdef(left.resultdef).iidguid) then
  1697. begin
  1698. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1699. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1700. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1701. end
  1702. else
  1703. internalerror(2013112914);
  1704. end;
  1705. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1706. begin
  1707. if (actualtargetnode(@left)^.nodetype in [pointerconstn,niln]) then
  1708. CGMessage(type_e_no_addr_of_constant);
  1709. { a dynamic array is a pointer to an array, so to convert it to }
  1710. { an open array, we have to dereference it (JM) }
  1711. result:=ctypeconvnode.create_internal(left,cpointerdef.getreusable(resultdef));
  1712. typecheckpass(result);
  1713. { left is reused }
  1714. left:=nil;
  1715. result:=cderefnode.create(result);
  1716. include(TDerefNode(result).derefnodeflags,drnf_no_checkpointer);
  1717. end;
  1718. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1719. var
  1720. newblock : tblocknode;
  1721. newstat : tstatementnode;
  1722. restemp : ttempcreatenode;
  1723. begin
  1724. if tstringdef(resultdef).stringtype=st_shortstring then
  1725. begin
  1726. newblock:=internalstatements(newstat);
  1727. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1728. addstatement(newstat,restemp);
  1729. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1730. ctemprefnode.create(restemp),nil))));
  1731. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1732. addstatement(newstat,ctemprefnode.create(restemp));
  1733. result:=newblock;
  1734. end
  1735. else if tstringdef(resultdef).stringtype=st_ansistring then
  1736. begin
  1737. result:=ccallnode.createinternres(
  1738. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1739. ccallparanode.create(
  1740. cordconstnode.create(
  1741. getparaencoding(resultdef),
  1742. u16inttype,
  1743. true
  1744. ),
  1745. ccallparanode.create(left,nil)
  1746. ),
  1747. resultdef
  1748. );
  1749. end
  1750. else
  1751. result := ccallnode.createinternres(
  1752. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1753. ccallparanode.create(left,nil),resultdef);
  1754. left:=nil;
  1755. end;
  1756. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1757. begin
  1758. result := ccallnode.createinternres(
  1759. 'fpc_variant_to_dynarray',
  1760. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1761. ccallparanode.create(left,nil)
  1762. ),resultdef);
  1763. typecheckpass(result);
  1764. left:=nil;
  1765. end;
  1766. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1767. begin
  1768. result := ccallnode.createinternres(
  1769. 'fpc_dynarray_to_variant',
  1770. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1771. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1772. ),resultdef);
  1773. typecheckpass(result);
  1774. left:=nil;
  1775. end;
  1776. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1777. begin
  1778. if def_is_related(tobjectdef(resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1779. result := ccallnode.createinternres(
  1780. 'fpc_variant_to_idispatch',
  1781. ccallparanode.create(left,nil)
  1782. ,resultdef)
  1783. else
  1784. result := ccallnode.createinternres(
  1785. 'fpc_variant_to_interface',
  1786. ccallparanode.create(left,nil)
  1787. ,resultdef);
  1788. typecheckpass(result);
  1789. left:=nil;
  1790. end;
  1791. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1792. begin
  1793. if def_is_related(tobjectdef(left.resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1794. result := ccallnode.createinternres(
  1795. 'fpc_idispatch_to_variant',
  1796. ccallparanode.create(left,nil)
  1797. ,resultdef)
  1798. else
  1799. result := ccallnode.createinternres(
  1800. 'fpc_interface_to_variant',
  1801. ccallparanode.create(left,nil)
  1802. ,resultdef);
  1803. typecheckpass(result);
  1804. left:=nil;
  1805. end;
  1806. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1807. begin
  1808. result := ctypeconvnode.create_internal(left,sinttype);
  1809. result := ctypeconvnode.create_internal(result,resultdef);
  1810. typecheckpass(result);
  1811. { left is reused }
  1812. left := nil;
  1813. end;
  1814. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1815. begin
  1816. result := ctypeconvnode.create_internal(left,sinttype);
  1817. result := ctypeconvnode.create_internal(result,cvarianttype);
  1818. typecheckpass(result);
  1819. { left is reused }
  1820. left := nil;
  1821. end;
  1822. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1823. var
  1824. newstatement : tstatementnode;
  1825. temp : ttempcreatenode;
  1826. temp2 : ttempcreatenode;
  1827. begin
  1828. { create statements with call to getmem+initialize }
  1829. result:=internalstatements(newstatement);
  1830. { create temp for result }
  1831. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1832. addstatement(newstatement,temp);
  1833. { get temp for array of lengths }
  1834. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1835. addstatement(newstatement,temp2);
  1836. { one dimensional }
  1837. addstatement(newstatement,cassignmentnode.create(
  1838. ctemprefnode.create(temp2),
  1839. cordconstnode.create
  1840. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1841. { create call to fpc_dynarr_setlength }
  1842. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1843. ccallparanode.create(caddrnode.create_internal
  1844. (ctemprefnode.create(temp2)),
  1845. ccallparanode.create(cordconstnode.create
  1846. (1,s32inttype,true),
  1847. ccallparanode.create(caddrnode.create_internal
  1848. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1849. ccallparanode.create(
  1850. ctypeconvnode.create_internal(
  1851. ctemprefnode.create(temp),voidpointertype),
  1852. nil))))
  1853. ));
  1854. addstatement(newstatement,ctempdeletenode.create(temp2));
  1855. { copy ... }
  1856. addstatement(newstatement,cassignmentnode.create(
  1857. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1858. left
  1859. ));
  1860. { left is reused }
  1861. left:=nil;
  1862. { the last statement should return the value as
  1863. location and type, this is done be referencing the
  1864. temp and converting it first from a persistent temp to
  1865. normal temp }
  1866. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1867. addstatement(newstatement,ctemprefnode.create(temp));
  1868. end;
  1869. function ttypeconvnode.typecheck_elem_2_openarray : tnode;
  1870. begin
  1871. result:=nil;
  1872. end;
  1873. function ttypeconvnode.typecheck_arrayconstructor_to_dynarray : tnode;
  1874. var
  1875. newstatement,
  1876. assstatement : tstatementnode;
  1877. arrnode : ttempcreatenode;
  1878. temp2 : ttempcreatenode;
  1879. assnode : tnode;
  1880. paracount : integer;
  1881. elemnode : tarrayconstructornode;
  1882. begin
  1883. { assignment of []? }
  1884. if (
  1885. (left.nodetype=arrayconstructorn) and
  1886. not assigned(tarrayconstructornode(left).left)
  1887. ) or
  1888. is_emptyset(left)
  1889. then
  1890. begin
  1891. result:=cnilnode.create;
  1892. exit;
  1893. end;
  1894. if resultdef.typ<>arraydef then
  1895. internalerror(2017050102);
  1896. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1897. result:=internalstatements(newstatement);
  1898. { create temp for result }
  1899. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1900. addstatement(newstatement,arrnode);
  1901. paracount:=0;
  1902. { create an assignment call for each element }
  1903. assnode:=internalstatements(assstatement);
  1904. if left.nodetype=arrayconstructorrangen then
  1905. internalerror(2016021902);
  1906. elemnode:=tarrayconstructornode(left);
  1907. while assigned(elemnode) do
  1908. begin
  1909. { arr[i] := param_i }
  1910. if not assigned(elemnode.left) then
  1911. internalerror(2017050103);
  1912. addstatement(assstatement,
  1913. cassignmentnode.create(
  1914. cvecnode.create(
  1915. ctemprefnode.create(arrnode),
  1916. cordconstnode.create(paracount,tarraydef(totypedef).rangedef,false)),
  1917. elemnode.left));
  1918. elemnode.left:=nil;
  1919. inc(paracount);
  1920. elemnode:=tarrayconstructornode(elemnode.right);
  1921. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1922. internalerror(2016021903);
  1923. end;
  1924. { get temp for array of lengths }
  1925. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1926. addstatement(newstatement,temp2);
  1927. { create call to fpc_dynarr_setlength }
  1928. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1929. ccallparanode.create(caddrnode.create_internal
  1930. (ctemprefnode.create(temp2)),
  1931. ccallparanode.create(cordconstnode.create
  1932. (1,s32inttype,true),
  1933. ccallparanode.create(caddrnode.create_internal
  1934. (crttinode.create(tstoreddef(totypedef),initrtti,rdt_normal)),
  1935. ccallparanode.create(
  1936. ctypeconvnode.create_internal(
  1937. ctemprefnode.create(arrnode),voidpointertype),
  1938. nil))))
  1939. ));
  1940. { add assignment statememnts }
  1941. addstatement(newstatement,ctempdeletenode.create(temp2));
  1942. addstatement(newstatement,assnode);
  1943. { the last statement should return the value as
  1944. location and type, this is done be referencing the
  1945. temp and converting it first from a persistent temp to
  1946. normal temp }
  1947. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1948. addstatement(newstatement,ctemprefnode.create(arrnode));
  1949. end;
  1950. function ttypeconvnode.typecheck_arrayconstructor_to_array : tnode;
  1951. var
  1952. newstatement,
  1953. assstatement : tstatementnode;
  1954. arrnode : ttempcreatenode;
  1955. temp2 : ttempcreatenode;
  1956. assnode : tnode;
  1957. paracount : integer;
  1958. elemnode : tarrayconstructornode;
  1959. begin
  1960. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1961. result:=internalstatements(newstatement);
  1962. { create temp for result }
  1963. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1964. addstatement(newstatement,arrnode);
  1965. paracount:=0;
  1966. { create an assignment call for each element }
  1967. assnode:=internalstatements(assstatement);
  1968. if left.nodetype=arrayconstructorrangen then
  1969. internalerror(2020041402);
  1970. elemnode:=tarrayconstructornode(left);
  1971. while assigned(elemnode) do
  1972. begin
  1973. { arr[i] := param_i }
  1974. if not assigned(elemnode.left) then
  1975. internalerror(2020041403);
  1976. addstatement(assstatement,
  1977. cassignmentnode.create(
  1978. cvecnode.create(
  1979. ctemprefnode.create(arrnode),
  1980. cordconstnode.create(paracount+tarraydef(totypedef).lowrange,tarraydef(totypedef).rangedef,false)),
  1981. elemnode.left));
  1982. elemnode.left:=nil;
  1983. inc(paracount);
  1984. elemnode:=tarrayconstructornode(elemnode.right);
  1985. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1986. internalerror(2020041404);
  1987. end;
  1988. { get temp for array of lengths }
  1989. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1990. addstatement(newstatement,temp2);
  1991. { add assignment statememnts }
  1992. addstatement(newstatement,ctempdeletenode.create(temp2));
  1993. addstatement(newstatement,assnode);
  1994. { the last statement should return the value as
  1995. location and type, this is done be referencing the
  1996. temp and converting it first from a persistent temp to
  1997. normal temp }
  1998. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1999. addstatement(newstatement,ctemprefnode.create(arrnode));
  2000. end;
  2001. function ttypeconvnode._typecheck_int_to_int : tnode;
  2002. begin
  2003. result := typecheck_int_to_int;
  2004. end;
  2005. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  2006. begin
  2007. result := typecheck_cord_to_pointer;
  2008. end;
  2009. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  2010. begin
  2011. result := typecheck_chararray_to_string;
  2012. end;
  2013. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  2014. begin
  2015. result := typecheck_string_to_chararray;
  2016. end;
  2017. function ttypeconvnode._typecheck_string_to_string: tnode;
  2018. begin
  2019. result := typecheck_string_to_string;
  2020. end;
  2021. function ttypeconvnode._typecheck_char_to_string : tnode;
  2022. begin
  2023. result := typecheck_char_to_string;
  2024. end;
  2025. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  2026. begin
  2027. result := typecheck_char_to_chararray;
  2028. end;
  2029. function ttypeconvnode._typecheck_int_to_real : tnode;
  2030. begin
  2031. result := typecheck_int_to_real;
  2032. end;
  2033. function ttypeconvnode._typecheck_real_to_real : tnode;
  2034. begin
  2035. result := typecheck_real_to_real;
  2036. end;
  2037. function ttypeconvnode._typecheck_real_to_currency : tnode;
  2038. begin
  2039. result := typecheck_real_to_currency;
  2040. end;
  2041. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  2042. begin
  2043. result := typecheck_cchar_to_pchar;
  2044. end;
  2045. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  2046. begin
  2047. result := typecheck_cstring_to_pchar;
  2048. end;
  2049. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  2050. begin
  2051. result := typecheck_cstring_to_int;
  2052. end;
  2053. function ttypeconvnode._typecheck_char_to_char : tnode;
  2054. begin
  2055. result := typecheck_char_to_char;
  2056. end;
  2057. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  2058. begin
  2059. result := typecheck_arrayconstructor_to_set;
  2060. end;
  2061. function ttypeconvnode._typecheck_set_to_set : tnode;
  2062. begin
  2063. result := typecheck_set_to_set;
  2064. end;
  2065. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  2066. begin
  2067. result := typecheck_pchar_to_string;
  2068. end;
  2069. function ttypeconvnode._typecheck_interface_to_string : tnode;
  2070. begin
  2071. result := typecheck_interface_to_string;
  2072. end;
  2073. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  2074. begin
  2075. result := typecheck_interface_to_guid;
  2076. end;
  2077. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  2078. begin
  2079. result := typecheck_dynarray_to_openarray;
  2080. end;
  2081. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  2082. begin
  2083. result := typecheck_pwchar_to_string;
  2084. end;
  2085. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  2086. begin
  2087. result := typecheck_variant_to_dynarray;
  2088. end;
  2089. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  2090. begin
  2091. result := typecheck_dynarray_to_variant;
  2092. end;
  2093. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  2094. begin
  2095. result := typecheck_variant_to_enum;
  2096. end;
  2097. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  2098. begin
  2099. result := typecheck_enum_to_variant;
  2100. end;
  2101. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  2102. begin
  2103. result := typecheck_proc_to_procvar;
  2104. end;
  2105. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  2106. begin
  2107. result := typecheck_variant_to_interface;
  2108. end;
  2109. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  2110. begin
  2111. result := typecheck_interface_to_variant;
  2112. end;
  2113. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  2114. begin
  2115. result := typecheck_array_2_dynarray;
  2116. end;
  2117. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  2118. begin
  2119. result := typecheck_elem_2_openarray;
  2120. end;
  2121. function ttypeconvnode._typecheck_arrayconstructor_to_dynarray : tnode;
  2122. begin
  2123. result:=typecheck_arrayconstructor_to_dynarray;
  2124. end;
  2125. function ttypeconvnode._typecheck_arrayconstructor_to_array : tnode;
  2126. begin
  2127. result:=typecheck_arrayconstructor_to_array;
  2128. end;
  2129. function ttypeconvnode._typecheck_procvar_to_funcref : tnode;
  2130. begin
  2131. result:=typecheck_procvar_2_funcref;
  2132. end;
  2133. function ttypeconvnode._typecheck_anonproc_to_funcref : tnode;
  2134. begin
  2135. result:=typecheck_anonproc_2_funcref;
  2136. end;
  2137. function ttypeconvnode.target_specific_general_typeconv: boolean;
  2138. begin
  2139. result:=false;
  2140. end;
  2141. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  2142. begin
  2143. result:=false;
  2144. end;
  2145. class function ttypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  2146. begin
  2147. result:=false;
  2148. end;
  2149. type
  2150. tsym_mapping = record
  2151. oldsym:tsym;
  2152. newsym:tsym;
  2153. end;
  2154. psym_mapping = ^tsym_mapping;
  2155. function replace_self_sym(var n:tnode;arg:pointer):foreachnoderesult;
  2156. var
  2157. mapping : psym_mapping absolute arg;
  2158. ld : tloadnode;
  2159. begin
  2160. if n.nodetype=loadn then
  2161. begin
  2162. ld:=tloadnode(n);
  2163. if ld.symtableentry=mapping^.oldsym then
  2164. begin
  2165. ld.symtableentry:=mapping^.newsym;
  2166. { make sure that the node is processed again }
  2167. ld.resultdef:=nil;
  2168. if assigned(ld.left) then
  2169. begin
  2170. { no longer loaded through the frame pointer }
  2171. ld.left.free;
  2172. ld.left:=nil;
  2173. end;
  2174. typecheckpass(n);
  2175. end;
  2176. end;
  2177. result:=fen_true;
  2178. end;
  2179. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  2180. function is_self_sym(sym:tsym):boolean;
  2181. begin
  2182. result:=(sym.typ in [localvarsym,paravarsym]) and
  2183. (vo_is_self in tabstractvarsym(sym).varoptions);
  2184. end;
  2185. var
  2186. pd : tabstractprocdef;
  2187. copytype : tproccopytyp;
  2188. source: pnode;
  2189. fpsym,
  2190. selfsym,
  2191. sym : tsym;
  2192. mapping : tsym_mapping;
  2193. pi : tprocinfo;
  2194. i : longint;
  2195. begin
  2196. result:=nil;
  2197. pd:=tabstractprocdef(left.resultdef);
  2198. { create procvardef (default for create_proc_to_procvar is voiddef,
  2199. but if later a regular inserttypeconvnode() is used to insert a type
  2200. conversion to the actual procvardef, totypedef will be set to the
  2201. real procvartype that we are converting to) }
  2202. if assigned(totypedef) and
  2203. (totypedef.typ=procvardef) then
  2204. begin
  2205. { have to do this in typecheckpass so that it's triggered for
  2206. typed constant declarations }
  2207. if po_is_block in tprocvardef(totypedef).procoptions then
  2208. begin
  2209. { can only convert from procdef to procvardef, but in the mean
  2210. time other type conversions may have been inserted (pointers,
  2211. proc2procvar, ...) }
  2212. source:=actualtargetnode(@left);
  2213. while (source^.nodetype=typeconvn) and
  2214. (ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
  2215. (is_void(source^.resultdef) or
  2216. (source^.resultdef.typ=procvardef)) do
  2217. begin
  2218. { won't skip proc2procvar }
  2219. source:=actualtargetnode(@ttypeconvnode(source^).left);
  2220. end;
  2221. if (source^.nodetype=loadn) and
  2222. (source^.resultdef.typ=procdef) and
  2223. not is_nested_pd(tprocdef(source^.resultdef)) and
  2224. not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
  2225. begin
  2226. result:=generate_block_for_procaddr(tloadnode(source^));
  2227. exit;
  2228. end
  2229. else
  2230. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2231. end
  2232. else if (pd.typ=procdef) and
  2233. (po_anonymous in pd.procoptions) then
  2234. begin
  2235. if left.nodetype<>loadn then
  2236. internalerror(2021062402);
  2237. { get rid of any potential framepointer loading; if it's necessary
  2238. (for a nested procvar for example) it will be added again }
  2239. if assigned(tloadnode(left).left) and (tloadnode(left).left.nodetype=loadparentfpn) then
  2240. begin
  2241. tloadnode(left).left.free;
  2242. tloadnode(left).left:=nil;
  2243. tloadnode(left).resultdef:=nil;
  2244. end;
  2245. if tprocvardef(totypedef).is_methodpointer then
  2246. begin
  2247. if assigned(tprocdef(pd).capturedsyms) and
  2248. (
  2249. (tprocdef(pd).capturedsyms.count>1) or
  2250. (
  2251. (tprocdef(pd).capturedsyms.count=1) and
  2252. not is_self_sym(tsym(pcapturedsyminfo(tprocdef(pd).capturedsyms[0])^.sym))
  2253. )
  2254. ) then
  2255. begin
  2256. result:=cerrornode.create;
  2257. exit;
  2258. end;
  2259. { so that insert_self_and_vmt_para correctly inserts the
  2260. Self, cause it otherwise skips that for anonymous functions }
  2261. include(pd.procoptions,po_methodpointer);
  2262. { we know this only captures Self, so we can move the
  2263. anonymous function to normal function level }
  2264. pd.parast.symtablelevel:=normal_function_level;
  2265. tprocdef(pd).localst.symtablelevel:=normal_function_level;
  2266. selfsym:=nil;
  2267. fpsym:=nil;
  2268. { find the framepointer parameter and an eventual self }
  2269. for i:=0 to tprocdef(pd).parast.symlist.count-1 do
  2270. begin
  2271. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2272. if sym.typ<>paravarsym then
  2273. continue;
  2274. if vo_is_parentfp in tparavarsym(sym).varoptions then
  2275. fpsym:=sym;
  2276. if vo_is_self in tparavarsym(sym).varoptions then
  2277. selfsym:=sym;
  2278. if assigned(fpsym) and assigned(selfsym) then
  2279. break;
  2280. end;
  2281. if assigned(fpsym) then
  2282. tprocdef(pd).parast.symlist.remove(fpsym);
  2283. { if we don't have a self parameter already we need to
  2284. insert a suitable one }
  2285. if not assigned(selfsym) then
  2286. begin
  2287. { replace the self symbol by the new parameter if it was
  2288. captured }
  2289. if assigned(tprocdef(pd).capturedsyms) and
  2290. (tprocdef(pd).capturedsyms.count>0) then
  2291. begin
  2292. if not assigned(tprocdef(pd).struct) then
  2293. { we can't use the captured symbol for the struct as that
  2294. might be the self of a type helper, thus we need to find
  2295. the parent procinfo that provides the Self }
  2296. tprocdef(pd).struct:=current_procinfo.get_normal_proc.procdef.struct;
  2297. if not assigned(tprocdef(pd).struct) then
  2298. internalerror(2021062204);
  2299. insert_self_and_vmt_para(pd);
  2300. mapping.oldsym:=tsym(pcapturedsyminfo(tprocdef(pd).capturedsyms[0])^.sym);
  2301. mapping.newsym:=nil;
  2302. { find the new self parameter }
  2303. for i:=0 to tprocdef(pd).parast.symlist.count-1 do
  2304. begin
  2305. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2306. if (sym.typ=paravarsym) and (vo_is_self in tparavarsym(sym).varoptions) then
  2307. begin
  2308. mapping.newsym:=sym;
  2309. break;
  2310. end;
  2311. end;
  2312. if not assigned(mapping.newsym) then
  2313. internalerror(2021062202);
  2314. { the anonymous function can only be a direct child of the
  2315. current_procinfo }
  2316. pi:=current_procinfo.get_first_nestedproc;
  2317. while assigned(pi) do
  2318. begin
  2319. if pi.procdef=pd then
  2320. break;
  2321. pi:=tprocinfo(pi.next);
  2322. end;
  2323. if not assigned(pi) then
  2324. internalerror(2021062203);
  2325. { replace all uses of the captured Self by the new Self
  2326. parameter }
  2327. foreachnodestatic(pm_preprocess,tcgprocinfo(pi).code,@replace_self_sym,@mapping);
  2328. end
  2329. else
  2330. begin
  2331. { for a nested function of a method struct is already
  2332. set }
  2333. if not assigned(tprocdef(pd).struct) then
  2334. { simply add a TObject as Self parameter }
  2335. tprocdef(pd).struct:=class_tobject;
  2336. insert_self_and_vmt_para(pd);
  2337. { there is no self, so load a nil value }
  2338. tloadnode(left).set_mp(cnilnode.create);
  2339. end;
  2340. end;
  2341. { the anonymous function no longer adheres to the nested
  2342. calling convention }
  2343. exclude(pd.procoptions,po_delphi_nested_cc);
  2344. tprocdef(pd).calcparas;
  2345. if not assigned(tloadnode(left).left) then
  2346. tloadnode(left).set_mp(load_self_node);
  2347. end
  2348. else if tprocvardef(totypedef).is_addressonly then
  2349. begin
  2350. if assigned(tprocdef(pd).capturedsyms) and (tprocdef(pd).capturedsyms.count>0) then
  2351. begin
  2352. result:=cerrornode.create;
  2353. exit;
  2354. end;
  2355. { remove framepointer and Self parameters }
  2356. for i:=tprocdef(pd).parast.symlist.count-1 downto 0 do
  2357. begin
  2358. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2359. if (sym.typ=paravarsym) and (tparavarsym(sym).varoptions*[vo_is_parentfp,vo_is_self]<>[]) then
  2360. tprocdef(pd).parast.symlist.delete(i);
  2361. end;
  2362. { the anonymous function no longer adheres to the nested
  2363. calling convention }
  2364. exclude(pd.procoptions,po_delphi_nested_cc);
  2365. { we don't need to look through the existing nodes, cause
  2366. the parameter was never used anyway }
  2367. tprocdef(pd).calcparas;
  2368. end
  2369. else
  2370. begin
  2371. { this is a nested function pointer, so ensure that the
  2372. anonymous function is handled as such }
  2373. if assigned(tprocdef(pd).capturedsyms) and
  2374. (tprocdef(pd).capturedsyms.count>0) and
  2375. (left.nodetype=loadn) then
  2376. begin
  2377. tloadnode(left).left:=cloadparentfpnode.create(tprocdef(tloadnode(left).symtable.defowner),lpf_forload);
  2378. typecheckpass(tloadnode(left).left);
  2379. pi:=current_procinfo.get_first_nestedproc;
  2380. while assigned(pi) do
  2381. begin
  2382. if pi.procdef=pd then
  2383. break;
  2384. pi:=tprocinfo(pi.next);
  2385. end;
  2386. pi.set_needs_parentfp(tprocdef(tloadnode(left).symtable.defowner).parast.symtablelevel);
  2387. end;
  2388. end;
  2389. end;
  2390. resultdef:=totypedef;
  2391. end
  2392. else
  2393. begin
  2394. { only need the address of the method? this is needed
  2395. for @tobject.create. In this case there will be a loadn without
  2396. a methodpointer. }
  2397. if (left.nodetype=loadn) and
  2398. not assigned(tloadnode(left).left) and
  2399. (not(m_nested_procvars in current_settings.modeswitches) or
  2400. not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
  2401. copytype:=pc_address_only
  2402. else
  2403. copytype:=pc_normal;
  2404. resultdef:=cprocvardef.getreusableprocaddr(pd,copytype);
  2405. end;
  2406. end;
  2407. function ttypeconvnode.typecheck_procvar_2_funcref : tnode;
  2408. var
  2409. capturer : tsym;
  2410. intfdef : tdef;
  2411. ld,blck,hp : tnode;
  2412. stmt : tstatementnode;
  2413. begin
  2414. result:=nil;
  2415. if not(m_tp_procvar in current_settings.modeswitches) and
  2416. is_invokable(resultdef) and
  2417. (left.nodetype=typeconvn) and
  2418. (ttypeconvnode(left).convtype=tc_proc_2_procvar) and
  2419. is_methodpointer(left.resultdef) and
  2420. (po_classmethod in tprocvardef(left.resultdef).procoptions) and
  2421. not(po_staticmethod in tprocvardef(left.resultdef).procoptions) and
  2422. (proc_to_funcref_equal(tprocdef(ttypeconvnode(left).left.resultdef),tobjectdef(resultdef))>=te_convert_l1) then
  2423. begin
  2424. hp:=left;
  2425. left:=ttypeconvnode(left).left;
  2426. if (left.nodetype=loadn) and
  2427. not assigned(tloadnode(left).left) then
  2428. tloadnode(left).set_mp(cloadvmtaddrnode.create(ctypenode.create(tdef(tloadnode(left).symtable.defowner))));
  2429. left:=ctypeconvnode.create_proc_to_procvar(left);
  2430. ttypeconvnode(left).totypedef:=resultdef;
  2431. typecheckpass(left);
  2432. ttypeconvnode(hp).left:=nil;
  2433. hp.free;
  2434. hp := nil;
  2435. end;
  2436. intfdef:=capturer_add_procvar_or_proc(current_procinfo,left,capturer,hp);
  2437. if assigned(intfdef) then
  2438. begin
  2439. if assigned(capturer) then
  2440. ld:=cloadnode.create(capturer,capturer.owner)
  2441. else
  2442. ld:=cnilnode.create;
  2443. result:=ctypeconvnode.create_internal(
  2444. ctypeconvnode.create_internal(
  2445. ld,
  2446. intfdef),
  2447. totypedef);
  2448. if assigned(hp) then
  2449. begin
  2450. blck:=internalstatements(stmt);
  2451. addstatement(stmt,cassignmentnode.create(hp,left));
  2452. left:=nil;
  2453. addstatement(stmt,result);
  2454. result:=blck;
  2455. end;
  2456. end;
  2457. if not assigned(result) then
  2458. result:=cerrornode.create;
  2459. end;
  2460. function ttypeconvnode.typecheck_anonproc_2_funcref : tnode;
  2461. var
  2462. capturer : tsym;
  2463. intfdef : tdef;
  2464. ldnode : tnode;
  2465. begin
  2466. intfdef:=capturer_add_anonymous_proc(current_procinfo,tprocdef(left.resultdef),capturer);
  2467. if assigned(intfdef) then
  2468. begin
  2469. if assigned(capturer) then
  2470. ldnode:=cloadnode.create(capturer,capturer.owner)
  2471. else
  2472. ldnode:=cnilnode.create;
  2473. result:=ctypeconvnode.create_internal(
  2474. ctypeconvnode.create_internal(
  2475. ldnode,
  2476. intfdef),
  2477. totypedef);
  2478. end
  2479. else
  2480. result:=cerrornode.create;
  2481. end;
  2482. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  2483. const
  2484. resultdefconvert : array[tconverttype] of pointer = (
  2485. {none} nil,
  2486. {equal} nil,
  2487. {not_possible} nil,
  2488. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  2489. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  2490. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  2491. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  2492. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  2493. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  2494. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  2495. { ansistring_2_pchar } nil,
  2496. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  2497. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  2498. { array_2_pointer } nil,
  2499. { pointer_2_array } nil,
  2500. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  2501. { int_2_bool } nil,
  2502. { bool_2_bool } nil,
  2503. { bool_2_int } nil,
  2504. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  2505. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  2506. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  2507. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  2508. { nil_2_methodprocvar } nil,
  2509. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  2510. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  2511. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  2512. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  2513. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  2514. { class_2_intf } nil,
  2515. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  2516. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  2517. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  2518. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  2519. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  2520. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  2521. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  2522. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  2523. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  2524. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  2525. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray,
  2526. { arrayconstructor_2_dynarray } @ttypeconvnode._typecheck_arrayconstructor_to_dynarray,
  2527. { arrayconstructor_2_array } @ttypeconvnode._typecheck_arrayconstructor_to_array,
  2528. { anonproc_2_funcref } @ttypeconvnode._typecheck_anonproc_to_funcref,
  2529. { procvar_2_funcref } @ttypeconvnode._typecheck_procvar_to_funcref
  2530. );
  2531. type
  2532. tprocedureofobject = function : tnode of object;
  2533. var
  2534. r : TMethod;
  2535. begin
  2536. result:=nil;
  2537. { this is a little bit dirty but it works }
  2538. { and should be quite portable too }
  2539. r.Code:=resultdefconvert[c];
  2540. r.Data:=self;
  2541. if assigned(r.Code) then
  2542. result:=tprocedureofobject(r)();
  2543. end;
  2544. function ttypeconvnode.pass_typecheck:tnode;
  2545. var
  2546. hdef : tdef;
  2547. hp : tnode;
  2548. currprocdef : tabstractprocdef;
  2549. aprocdef : tprocdef;
  2550. eq : tequaltype;
  2551. cdoptions : tcompare_defs_options;
  2552. selfnode : tnode;
  2553. newblock: tblocknode;
  2554. newstatement: tstatementnode;
  2555. tempnode: ttempcreatenode;
  2556. begin
  2557. result:=nil;
  2558. resultdef:=totypedef;
  2559. typecheckpass(left);
  2560. if codegenerror then
  2561. exit;
  2562. { When absolute force tc_equal }
  2563. if (nf_absolute in flags) then
  2564. begin
  2565. convtype:=tc_equal;
  2566. { we need to check regability only if something is really regable }
  2567. if ((tstoreddef(left.resultdef).is_intregable) or
  2568. (tstoreddef(resultdef).is_fpuregable)) and
  2569. (
  2570. (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  2571. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) or
  2572. { like in pdecvar.read_absolute(): if the size changes, the
  2573. register size would also have to change (but second_nothing
  2574. does not handle this) }
  2575. (tstoreddef(resultdef).size<>tstoreddef(left.resultdef).size)) then
  2576. make_not_regable(left,[ra_addr_regable]);
  2577. exit;
  2578. end;
  2579. { tp procvar support. Skip typecasts to procvar, record or set. Those
  2580. convert on the procvar value. This is used to access the
  2581. fields of a methodpointer }
  2582. if not(nf_load_procvar in flags) and
  2583. not(resultdef.typ in [procvardef,recorddef,setdef]) and
  2584. not is_invokable(resultdef) and
  2585. { in case of interface assignments of invokables they'll be converted
  2586. to voidpointertype using an internal conversions; we must not call
  2587. the invokable in that case }
  2588. not (
  2589. (nf_internal in flags) and
  2590. is_invokable(left.resultdef)
  2591. ) then
  2592. maybe_call_procvar(left,true);
  2593. if target_specific_general_typeconv then
  2594. exit;
  2595. if convtype=tc_none then
  2596. begin
  2597. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  2598. { overloaded operators require calls, which is not possible inside
  2599. a constant declaration }
  2600. if (block_type<>bt_const) and
  2601. not(nf_internal in flags) then
  2602. include(cdoptions,cdo_check_operator);
  2603. if nf_explicit in flags then
  2604. include(cdoptions,cdo_explicit);
  2605. if nf_internal in flags then
  2606. include(cdoptions,cdo_internal);
  2607. aprocdef:=nil;
  2608. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  2609. case eq of
  2610. te_exact,
  2611. te_equal :
  2612. begin
  2613. result := simplify(false);
  2614. if assigned(result) then
  2615. exit;
  2616. { in case of bitpacked accesses, the original type must
  2617. remain so that not too many/few bits are laoded }
  2618. if is_bitpacked_access(left) then
  2619. convtype:=tc_int_2_int;
  2620. { Only leave when there is no conversion to do.
  2621. We can still need to call a conversion routine,
  2622. like the routine to convert a stringconstnode }
  2623. if (convtype in [tc_equal,tc_not_possible]) and
  2624. { some conversions, like dynarray to pointer in Delphi
  2625. mode, must not be removed, because then we get memory
  2626. leaks due to missing temp finalization }
  2627. (not is_managed_type(left.resultdef) or
  2628. { different kinds of refcounted types may need calls
  2629. to different kinds of refcounting helpers }
  2630. (resultdef=left.resultdef)) then
  2631. begin
  2632. {$ifdef llvm}
  2633. { we still may have to insert a type conversion at the
  2634. llvm level }
  2635. if (blocktype<>bt_const) and
  2636. (left.resultdef<>resultdef) and
  2637. { if unspecialised generic -> we won't generate any code
  2638. for this, and keeping the type conversion node will
  2639. cause valid_for_assign to fail because the typecast will be from/to something of 0
  2640. bytes to/from something with a non-zero size }
  2641. not is_typeparam(left.resultdef) and
  2642. not is_typeparam(resultdef) then
  2643. result:=nil
  2644. else
  2645. {$endif llvm}
  2646. begin
  2647. left.resultdef:=resultdef;
  2648. if (nf_explicit in flags) and (left.nodetype = addrn) then
  2649. include(taddrnode(left).addrnodeflags,anf_typedaddr);
  2650. result:=left;
  2651. left:=nil;
  2652. end;
  2653. exit;
  2654. end;
  2655. end;
  2656. te_convert_l1,
  2657. te_convert_l2,
  2658. te_convert_l3,
  2659. te_convert_l4,
  2660. te_convert_l5,
  2661. te_convert_l6:
  2662. { nothing to do }
  2663. ;
  2664. te_convert_operator :
  2665. begin
  2666. include(current_procinfo.flags,pi_do_call);
  2667. addsymref(aprocdef.procsym,aprocdef);
  2668. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[],nil);
  2669. { tell explicitly which def we must use !! (PM) }
  2670. tcallnode(hp).procdefinition:=aprocdef;
  2671. left:=nil;
  2672. result:=hp;
  2673. exit;
  2674. end;
  2675. te_incompatible :
  2676. begin
  2677. { convert an array constructor to a set so that we still get
  2678. the error "set of Y incompatible to Z" instead of "array of
  2679. X incompatible to Z" }
  2680. if (resultdef.typ<>arraydef) and
  2681. is_array_constructor(left.resultdef) then
  2682. begin
  2683. arrayconstructor_to_set(left);
  2684. typecheckpass(left);
  2685. end;
  2686. { Procedures have a resultdef of voiddef and functions of their
  2687. own resultdef. They will therefore always be incompatible with
  2688. a procvar. Because isconvertable cannot check for procedures we
  2689. use an extra check for them.}
  2690. if (left.nodetype=calln) and
  2691. (tcallnode(left).required_para_count=0) and
  2692. (
  2693. (resultdef.typ=procvardef) or
  2694. is_invokable(resultdef)
  2695. ) and
  2696. (
  2697. (m_tp_procvar in current_settings.modeswitches) or
  2698. (m_mac_procvar in current_settings.modeswitches)
  2699. ) then
  2700. begin
  2701. if assigned(tcallnode(left).right) then
  2702. begin
  2703. { this is already a procvar, if it is really equal
  2704. is checked below }
  2705. convtype:=tc_equal;
  2706. hp:=tcallnode(left).right.getcopy;
  2707. currprocdef:=tabstractprocdef(hp.resultdef);
  2708. end
  2709. else
  2710. begin
  2711. if resultdef.typ=procvardef then
  2712. begin
  2713. convtype:=tc_proc_2_procvar;
  2714. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  2715. end
  2716. else
  2717. begin
  2718. convtype:=tc_procvar_2_funcref;
  2719. currprocdef:=tprocsym(tcallnode(left).symtableprocentry).find_procdef_byfuncrefdef(tobjectdef(resultdef));
  2720. end;
  2721. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  2722. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2723. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2724. begin
  2725. selfnode:=tcallnode(left).methodpointer;
  2726. if assigned(selfnode) then
  2727. begin
  2728. { in case the nodetype is a typen, avoid the internal error
  2729. in set_mp and instead let the code error out normally }
  2730. if selfnode.nodetype<>typen then
  2731. tloadnode(hp).set_mp(selfnode.getcopy)
  2732. end
  2733. else
  2734. tloadnode(hp).set_mp(load_self_node);
  2735. end;
  2736. typecheckpass(hp);
  2737. end;
  2738. left.free;
  2739. left:=hp;
  2740. { Now check if the procedure we are going to assign to
  2741. the procvar, is compatible with the procvar's type }
  2742. if not(nf_explicit in flags) and
  2743. (
  2744. (
  2745. (resultdef.typ=procvardef) and
  2746. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible)
  2747. ) or (
  2748. is_invokable(resultdef) and
  2749. (proc_to_funcref_equal(currprocdef,tobjectdef(resultdef))=te_incompatible)
  2750. )
  2751. ) then
  2752. IncompatibleTypes(left.resultdef,resultdef)
  2753. else
  2754. result:=typecheck_call_helper(convtype);
  2755. exit;
  2756. end
  2757. else if maybe_global_proc_to_nested(left,resultdef) or
  2758. maybe_classmethod_to_methodprocvar(left,resultdef) then
  2759. begin
  2760. result:=left;
  2761. left:=nil;
  2762. exit;
  2763. end;
  2764. { Handle explicit type conversions }
  2765. if nf_explicit in flags then
  2766. begin
  2767. { do common tc_equal cast, except when dealing with proc -> procvar
  2768. (may have to get rid of method pointer) }
  2769. if (left.resultdef.typ<>procdef) or
  2770. (resultdef.typ<>procvardef) then
  2771. convtype:=tc_equal
  2772. else
  2773. convtype:=tc_proc_2_procvar;
  2774. { ordinal constants can be resized to 1,2,4,8 bytes }
  2775. if (left.nodetype=ordconstn) then
  2776. begin
  2777. { Insert typeconv for ordinal to the correct size first on left, after
  2778. that the other conversion can be done }
  2779. hdef:=nil;
  2780. case longint(resultdef.size) of
  2781. 1 :
  2782. hdef:=s8inttype;
  2783. 2 :
  2784. hdef:=s16inttype;
  2785. 4 :
  2786. hdef:=s32inttype;
  2787. 8 :
  2788. hdef:=s64inttype;
  2789. end;
  2790. { we need explicit, because it can also be an enum }
  2791. if assigned(hdef) then
  2792. inserttypeconv_internal(left,hdef)
  2793. else
  2794. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2795. end;
  2796. { class/interface to class/interface, with checkobject support }
  2797. if is_class_or_interface_or_objc(resultdef) and
  2798. is_class_or_interface_or_objc(left.resultdef) then
  2799. begin
  2800. { check if the types are related }
  2801. if not(nf_internal in flags) and
  2802. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2803. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2804. begin
  2805. { Give an error when typecasting class to interface, this is compatible
  2806. with delphi }
  2807. if is_interface(resultdef) and
  2808. not is_interface(left.resultdef) then
  2809. CGMessage2(type_e_classes_not_related,
  2810. FullTypeName(left.resultdef,resultdef),
  2811. FullTypeName(resultdef,left.resultdef))
  2812. else
  2813. CGMessage2(type_w_classes_not_related,
  2814. FullTypeName(left.resultdef,resultdef),
  2815. FullTypeName(resultdef,left.resultdef))
  2816. end;
  2817. { Add runtime check? }
  2818. if not is_objc_class_or_protocol(resultdef) and
  2819. not is_objc_class_or_protocol(left.resultdef) and
  2820. (cs_check_object in current_settings.localswitches) and
  2821. not(nf_internal in flags) then
  2822. begin
  2823. { we can translate the typeconvnode to 'as' when
  2824. typecasting to a class or interface }
  2825. { we need to make sure the result can still be
  2826. passed as a var parameter }
  2827. newblock:=internalstatements(newstatement);
  2828. if (valid_for_var(left,false)) then
  2829. begin
  2830. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2831. addstatement(newstatement,tempnode);
  2832. addstatement(newstatement,cassignmentnode.create(
  2833. ctemprefnode.create(tempnode),
  2834. caddrnode.create_internal(left)));
  2835. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2836. end
  2837. else
  2838. begin
  2839. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2840. addstatement(newstatement,tempnode);
  2841. addstatement(newstatement,cassignmentnode.create(
  2842. ctemprefnode.create(tempnode),
  2843. left));
  2844. left:=ctemprefnode.create(tempnode);
  2845. end;
  2846. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2847. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2848. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2849. left:=nil;
  2850. result:=newblock;
  2851. exit;
  2852. end;
  2853. end
  2854. else
  2855. begin
  2856. { only if the same size or formal def, and }
  2857. { don't allow type casting of constants to }
  2858. { structured types }
  2859. if not(
  2860. (left.resultdef.typ=formaldef) or
  2861. {$ifdef jvm}
  2862. { enums /are/ class instances on the JVM
  2863. platform }
  2864. (((left.resultdef.typ=enumdef) and
  2865. (resultdef.typ=objectdef)) or
  2866. ((resultdef.typ=enumdef) and
  2867. (left.resultdef.typ=objectdef))) or
  2868. {$endif}
  2869. (
  2870. is_void(left.resultdef) and
  2871. (left.nodetype=derefn)
  2872. ) or
  2873. (
  2874. not(is_open_array(left.resultdef)) and
  2875. not(is_array_constructor(left.resultdef)) and
  2876. not(is_array_of_const(left.resultdef)) and
  2877. { if the from type is an anonymous function then
  2878. don't blindly convert it if the size is the same
  2879. as compare_defs_ext already determined that the
  2880. anonymous function is not compatible }
  2881. not(
  2882. (left.resultdef.typ=procdef) and
  2883. (po_anonymous in tprocdef(left.resultdef).procoptions)
  2884. ) and
  2885. (left.resultdef.size=resultdef.size) and
  2886. { disallow casts of const nodes }
  2887. (not is_constnode(left) or
  2888. { however, there are some exceptions }
  2889. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2890. filedef,variantdef,objectdef]) or
  2891. is_class_or_interface_or_objc(resultdef) or
  2892. { the softfloat code generates casts <const. float> to record }
  2893. (nf_internal in flags)
  2894. ))
  2895. )
  2896. ) then
  2897. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2898. else
  2899. begin
  2900. { perform target-specific explicit typecast
  2901. checks }
  2902. if target_specific_explicit_typeconv then
  2903. begin
  2904. result:=simplify(false);
  2905. exit;
  2906. end;
  2907. end;
  2908. end;
  2909. end
  2910. else
  2911. IncompatibleTypes(left.resultdef,resultdef);
  2912. end;
  2913. end;
  2914. end;
  2915. { Give hint or warning for unportable code, exceptions are
  2916. - typecasts from constants
  2917. - void }
  2918. if not(nf_internal in flags) and
  2919. (left.nodetype<>ordconstn) and
  2920. not(is_void(left.resultdef)) and
  2921. (((left.resultdef.typ=orddef) and
  2922. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2923. ((resultdef.typ=orddef) and
  2924. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2925. begin
  2926. {Converting pointers to signed integers is a bad idea. Warn.}
  2927. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2928. { Give a warning when sizes don't match, because then info will be lost }
  2929. if left.resultdef.size=resultdef.size then
  2930. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2931. else
  2932. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2933. end;
  2934. { tc_cord_2_pointer still requires a type check, which
  2935. simplify does not do }
  2936. if (convtype<>tc_cord_2_pointer) then
  2937. begin
  2938. result := simplify(false);
  2939. if assigned(result) then
  2940. exit;
  2941. end;
  2942. { now call the resultdef helper to do constant folding }
  2943. result:=typecheck_call_helper(convtype);
  2944. end;
  2945. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2946. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2947. this applies is powerpc64
  2948. }
  2949. {$ifndef CPUNO32BITOPS}
  2950. { checks whether we can safely remove typeconversions to bigger types
  2951. in case range and overflow checking are off, and in case
  2952. the result of this node tree is downcasted again to a
  2953. smaller type value afterwards,
  2954. the smaller types being allowed are described by validints, ordinal constants must fit into l..h
  2955. We do this on 64 bit CPUs as well, they benefit from it as well }
  2956. function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;const l,h : Tconstexprint): boolean;
  2957. var
  2958. gotminus1,
  2959. gotsigned,
  2960. gotunsigned,
  2961. gotdivmod: boolean;
  2962. { checks whether a node has an accepted resultdef, or originally
  2963. had one but was implicitly converted to s64bit }
  2964. function wasoriginallysmallerint(n: tnode): boolean;
  2965. begin
  2966. if (n.resultdef.typ<>orddef) then
  2967. exit(false);
  2968. gotsigned:=gotsigned or is_signed(n.resultdef);
  2969. gotunsigned:=gotunsigned or not(is_signed(n.resultdef));
  2970. { actually, we should only check right (denominator) nodes here, but
  2971. setting it always is a safe approximation }
  2972. if ((n.nodetype=ordconstn) and
  2973. (tordconstnode(n).value=-1)) then
  2974. gotminus1:=true;
  2975. if (torddef(n.resultdef).ordtype in validints) then
  2976. begin
  2977. if is_signed(n.resultdef) then
  2978. gotsint:=true;
  2979. exit(true);
  2980. end;
  2981. { type conv to a bigger int, we do not like to use? }
  2982. if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
  2983. { nf_explicit is also set for explicitly typecasted }
  2984. { ordconstn's }
  2985. ([nf_internal,nf_explicit]*n.flags=[]) and
  2986. { either a typeconversion node coming from a smaller type }
  2987. (((n.nodetype=typeconvn) and
  2988. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2989. (torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
  2990. { or an ordconstnode which has a smaller type}
  2991. ((n.nodetype=ordconstn) and
  2992. (tordconstnode(n).value>=l) and
  2993. (tordconstnode(n).value<=h))) then
  2994. begin
  2995. if ((n.nodetype=typeconvn) and
  2996. is_signed(ttypeconvnode(n).left.resultdef)) or
  2997. ((n.nodetype=ordconstn) and
  2998. (tordconstnode(n).value<0)) then
  2999. begin
  3000. gotsint:=true;
  3001. gotsigned:=true;
  3002. end
  3003. else
  3004. gotunsigned:=true;
  3005. exit(true);
  3006. end;
  3007. result:=false;
  3008. end;
  3009. function docheckremoveinttypeconvs(n: tnode): boolean;
  3010. begin
  3011. if wasoriginallysmallerint(n) then
  3012. exit(true);
  3013. case n.nodetype of
  3014. subn,orn,xorn:
  3015. begin
  3016. { the result could become negative in this case }
  3017. if n.nodetype=subn then
  3018. gotsint:=true;
  3019. result:=
  3020. docheckremoveinttypeconvs(tbinarynode(n).left) and
  3021. docheckremoveinttypeconvs(tbinarynode(n).right);
  3022. end;
  3023. unaryminusn:
  3024. begin
  3025. gotsint:=true;
  3026. result:=docheckremoveinttypeconvs(tunarynode(n).left);
  3027. end;
  3028. shrn:
  3029. begin
  3030. result:=wasoriginallysmallerint(tbinarynode(n).left) and
  3031. docheckremoveinttypeconvs(tbinarynode(n).right);
  3032. end;
  3033. notn:
  3034. result:=docheckremoveinttypeconvs(tunarynode(n).left);
  3035. addn,muln,divn,modn,andn,shln:
  3036. begin
  3037. if n.nodetype in [divn,modn] then
  3038. gotdivmod:=true;
  3039. result:=
  3040. (docheckremoveinttypeconvs(tbinarynode(n).left) and
  3041. docheckremoveinttypeconvs(tbinarynode(n).right) and
  3042. (not(n.nodetype in [modn,divn]) or (not(gotminus1)))
  3043. ) or
  3044. (
  3045. (n.nodetype=andn) and
  3046. (
  3047. { Right node is more likely to be a constant, so check
  3048. this one first }
  3049. wasoriginallysmallerint(tbinarynode(n).right) or
  3050. wasoriginallysmallerint(tbinarynode(n).left)
  3051. )
  3052. );
  3053. end;
  3054. else
  3055. result:=false;
  3056. end;
  3057. end;
  3058. begin { checkremove64bittypeconvs }
  3059. gotdivmod:=false;
  3060. gotsint:=false;
  3061. gotminus1:=false;
  3062. gotsigned:=false;
  3063. gotunsigned:=false;
  3064. result:=
  3065. docheckremoveinttypeconvs(n) and
  3066. (not(gotdivmod) or (gotsigned xor gotunsigned));
  3067. end;
  3068. { remove int type conversions and set the result to the given type }
  3069. procedure doremoveinttypeconvs(level : dword;var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
  3070. function SmallerOrSigned(def: tdef): Boolean;
  3071. begin
  3072. Result := (def.size < signedtype.size) or
  3073. (
  3074. (def.size = signedtype.size) and
  3075. is_signed(def)
  3076. )
  3077. end;
  3078. var
  3079. newblock: tblocknode;
  3080. newstatements: tstatementnode;
  3081. originaldivtree: tnode;
  3082. tempnode: ttempcreatenode;
  3083. NeedMinus1Check: Boolean;
  3084. begin
  3085. { we may not recurse into shr nodes:
  3086. dword1:=dword1+((dword2+dword3) shr 2);
  3087. while we can remove an extension on the outermost addition, we cannot
  3088. remove it from the shr
  3089. }
  3090. { Don't downsize into a division operation either, as the numerator can
  3091. be much larger than the result and non-linear properties prevent
  3092. accurate truncation; fixes #39646 [Kit] }
  3093. if (n.nodetype in [shrn,divn,modn]) and (level<>0) then
  3094. begin
  3095. inserttypeconv_internal(n,todef);
  3096. exit;
  3097. end;
  3098. case n.nodetype of
  3099. subn,addn,muln,divn,modn,xorn,andn,orn,shln,shrn:
  3100. begin
  3101. exclude(n.flags,nf_internal);
  3102. if not forceunsigned and
  3103. is_signed(n.resultdef) then
  3104. begin
  3105. originaldivtree:=nil;
  3106. NeedMinus1Check:=False;
  3107. if n.nodetype in [divn,modn] then
  3108. begin
  3109. { If the DIV operation is being downsized, we must explicitly check for a divisor of -1 }
  3110. NeedMinus1Check := True;
  3111. { If the operand size is equal or smaller, the -1 check isn't necessary }
  3112. if (
  3113. SmallerOrSigned(tbinarynode(n).left.resultdef) or
  3114. (
  3115. (tbinarynode(n).left.nodetype = typeconvn) and
  3116. SmallerOrSigned(ttypeconvnode(tbinarynode(n).left).left.resultdef)
  3117. )
  3118. ) and
  3119. (
  3120. SmallerOrSigned(tbinarynode(n).right.resultdef) or
  3121. (
  3122. (tbinarynode(n).right.nodetype = typeconvn) and
  3123. SmallerOrSigned(ttypeconvnode(tbinarynode(n).right).left.resultdef)
  3124. )
  3125. ) then
  3126. NeedMinus1Check := False;
  3127. end;
  3128. if NeedMinus1Check then
  3129. originaldivtree:=n.getcopy;
  3130. doremoveinttypeconvs(level+1,tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
  3131. doremoveinttypeconvs(level+1,tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
  3132. n.resultdef:=signedtype;
  3133. if NeedMinus1Check then
  3134. begin
  3135. newblock:=internalstatements(newstatements);
  3136. tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,true);
  3137. addstatement(newstatements,tempnode);
  3138. addstatement(newstatements,cifnode.create_internal(
  3139. caddnode.create_internal(equaln,tbinarynode(n).right.getcopy,cordconstnode.create(-1,n.resultdef,false)),
  3140. cassignmentnode.create_internal(
  3141. ctemprefnode.create(tempnode),
  3142. cmoddivnode.create(n.nodetype,tbinarynode(originaldivtree).left.getcopy,cordconstnode.create(-1,tbinarynode(originaldivtree).right.resultdef,false))
  3143. ),
  3144. cassignmentnode.create_internal(
  3145. ctemprefnode.create(tempnode),n
  3146. )
  3147. )
  3148. );
  3149. addstatement(newstatements,ctempdeletenode.create_normal_temp(tempnode));
  3150. addstatement(newstatements,ctemprefnode.create(tempnode));
  3151. n:=newblock;
  3152. do_typecheckpass(n);
  3153. originaldivtree.free;
  3154. originaldivtree := nil;
  3155. end;
  3156. end
  3157. else
  3158. begin
  3159. doremoveinttypeconvs(level+1,tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3160. doremoveinttypeconvs(level+1,tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3161. n.resultdef:=unsignedtype;
  3162. end;
  3163. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  3164. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  3165. // ) then
  3166. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  3167. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  3168. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  3169. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  3170. end;
  3171. unaryminusn,notn:
  3172. begin
  3173. exclude(n.flags,nf_internal);
  3174. if not forceunsigned and
  3175. is_signed(n.resultdef) then
  3176. begin
  3177. doremoveinttypeconvs(level+1,tunarynode(n).left,signedtype,false,signedtype,unsignedtype);
  3178. n.resultdef:=signedtype;
  3179. end
  3180. else
  3181. begin
  3182. doremoveinttypeconvs(level+1,tunarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3183. n.resultdef:=unsignedtype;
  3184. end;
  3185. end;
  3186. typeconvn:
  3187. begin
  3188. ttypeconvnode(n).totypedef:=todef;
  3189. { may change the type conversion, e.g. if the old conversion was
  3190. from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
  3191. n.resultdef:=nil;
  3192. ttypeconvnode(n).convtype:=tc_none;
  3193. typecheckpass(n);
  3194. end;
  3195. else
  3196. inserttypeconv_internal(n,todef);
  3197. end;
  3198. end;
  3199. {$endif not CPUNO32BITOPS}
  3200. procedure swap_const_value (var val : TConstExprInt; size : longint);
  3201. begin
  3202. case size of
  3203. 1 : {do nothing };
  3204. 2 : if val.signed then
  3205. val.svalue:=swapendian(smallint(val.svalue))
  3206. else
  3207. val.uvalue:=swapendian(word(val.uvalue));
  3208. 4 : if val.signed then
  3209. val.svalue:=swapendian(longint(val.svalue))
  3210. else
  3211. val.uvalue:=swapendian(qword(val.uvalue));
  3212. 8 : if val.signed then
  3213. val.svalue:=swapendian(int64(val.svalue))
  3214. else
  3215. val.uvalue:=swapendian(qword(val.uvalue));
  3216. else
  3217. internalerror(2014111201);
  3218. end;
  3219. end;
  3220. function ttypeconvnode.simplify(forinline : boolean): tnode;
  3221. var
  3222. hp: tnode;
  3223. v: Tconstexprint;
  3224. {$ifndef CPUNO32BITOPS}
  3225. foundsint: boolean;
  3226. {$endif not CPUNO32BITOPS}
  3227. begin
  3228. result := nil;
  3229. { Constant folding and other node transitions to
  3230. remove the typeconv node }
  3231. case left.nodetype of
  3232. stringconstn :
  3233. if (resultdef.typ=stringdef) and
  3234. ((convtype=tc_equal) or
  3235. ((convtype=tc_string_2_string) and
  3236. (
  3237. ((not is_widechararray(left.resultdef) and
  3238. not is_wide_or_unicode_string(left.resultdef)) or
  3239. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  3240. )
  3241. )
  3242. )
  3243. ) then
  3244. begin
  3245. { output string consts in local ansistring encoding }
  3246. if is_ansistring(resultdef) and
  3247. { do not mess with the result type for internally created nodes }
  3248. not(nf_internal in flags) and
  3249. ((tstringdef(resultdef).encoding=0) or (tstringdef(resultdef).encoding=globals.CP_NONE)) then
  3250. tstringconstnode(left).changestringtype(getansistringdef)
  3251. else
  3252. tstringconstnode(left).changestringtype(resultdef);
  3253. result:=left;
  3254. left:=nil;
  3255. exit;
  3256. end
  3257. else if
  3258. (convtype<>tc_cstring_2_pchar) and
  3259. is_dynamicstring(left.resultdef) and
  3260. (tstringconstnode(left).len=0) and
  3261. (resultdef.typ=pointerdef) and
  3262. cstringconstnode.emptydynstrnil then
  3263. begin
  3264. result:=cnilnode.create;
  3265. exit;
  3266. end;
  3267. realconstn :
  3268. begin
  3269. if (convtype = tc_real_2_currency) then
  3270. result := typecheck_real_to_currency
  3271. else if (convtype = tc_real_2_real) then
  3272. result := typecheck_real_to_real
  3273. else
  3274. exit;
  3275. if not(assigned(result)) then
  3276. begin
  3277. result := left;
  3278. left := nil;
  3279. end;
  3280. if (result.nodetype = realconstn) then
  3281. begin
  3282. hp:=result;
  3283. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  3284. if nf_is_currency in hp.flags then
  3285. include(result.flags,nf_is_currency);
  3286. if ([nf_explicit,nf_internal] * flags <> []) then
  3287. include(result.flags, nf_explicit);
  3288. hp.free;
  3289. hp := nil;
  3290. end;
  3291. end;
  3292. niln :
  3293. begin
  3294. { nil to ordinal node }
  3295. if (resultdef.typ=orddef) then
  3296. begin
  3297. hp:=cordconstnode.create(0,resultdef,true);
  3298. if ([nf_explicit,nf_internal] * flags <> []) then
  3299. include(hp.flags, nf_explicit);
  3300. result:=hp;
  3301. exit;
  3302. end
  3303. else
  3304. { fold nil to any pointer type }
  3305. if (resultdef.typ=pointerdef) then
  3306. begin
  3307. hp:=cnilnode.create;
  3308. hp.resultdef:=resultdef;
  3309. if ([nf_explicit,nf_internal] * flags <> []) then
  3310. include(hp.flags, nf_explicit);
  3311. result:=hp;
  3312. exit;
  3313. end
  3314. else
  3315. { remove typeconv after niln, but not when the result is a
  3316. methodpointer. The typeconv of the methodpointer will then
  3317. take care of updateing size of niln to OS_64 }
  3318. if not((resultdef.typ=procvardef) and
  3319. not(tprocvardef(resultdef).is_addressonly)) and
  3320. { converting (dynamic array) nil to a an open array is not allowed }
  3321. not is_open_array(resultdef) then
  3322. begin
  3323. left.resultdef:=resultdef;
  3324. if ([nf_explicit,nf_internal] * flags <> []) then
  3325. include(left.flags, nf_explicit);
  3326. result:=left;
  3327. left:=nil;
  3328. exit;
  3329. end;
  3330. end;
  3331. ordconstn :
  3332. begin
  3333. { ordinal contants can be directly converted }
  3334. { but not char to char because it is a widechar to char or via versa }
  3335. { which needs extra code to do the code page transistion }
  3336. { constant ordinal to pointer }
  3337. if (resultdef.typ=pointerdef) and
  3338. (convtype<>tc_cchar_2_pchar) then
  3339. begin
  3340. if (target_info.system in systems_managed_vm) and
  3341. (tordconstnode(left).value<>0) then
  3342. message(parser_e_feature_unsupported_for_vm);
  3343. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  3344. if ([nf_explicit,nf_internal] * flags <> []) then
  3345. include(hp.flags, nf_explicit);
  3346. result:=hp;
  3347. exit;
  3348. end
  3349. else if is_ordinal(resultdef) and
  3350. not(convtype=tc_char_2_char) then
  3351. begin
  3352. { replace the resultdef and recheck the range }
  3353. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  3354. include(left.flags, nf_explicit)
  3355. else
  3356. { no longer an ordconst with an explicit typecast }
  3357. exclude(left.flags, nf_explicit);
  3358. { when converting from one boolean type to another, force }
  3359. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  3360. { (Delphi-compatibile) }
  3361. if is_boolean(left.resultdef) and
  3362. is_boolean(resultdef) and
  3363. (is_cbool(left.resultdef) or
  3364. is_cbool(resultdef)) then
  3365. begin
  3366. if is_pasbool(resultdef) then
  3367. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  3368. else
  3369. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  3370. end
  3371. else
  3372. begin
  3373. { for constant values on absolute variables, swapping is required }
  3374. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  3375. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  3376. if not(nf_generic_para in flags) then
  3377. adaptrange(
  3378. resultdef,tordconstnode(left).value,
  3379. { when evaluating an explicit typecast during inlining, don't warn about
  3380. lost bits; only warn if someone literally typed e.g. byte($1ff) }
  3381. (([nf_internal,nf_absolute]*flags)<>[]) or (forinline and (nf_explicit in flags)),
  3382. nf_explicit in flags,
  3383. cs_check_range in localswitches);
  3384. { swap value back, but according to new type }
  3385. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  3386. swap_const_value(tordconstnode(left).value,resultdef.size);
  3387. { cut off the new value? }
  3388. if resultdef.size<left.resultdef.size then
  3389. case resultdef.size of
  3390. 1:
  3391. if is_signed(resultdef) then
  3392. tordconstnode(left).value:=tordconstnode(left).value and shortint($ff)
  3393. else
  3394. tordconstnode(left).value:=tordconstnode(left).value and byte($ff);
  3395. 2:
  3396. if is_signed(resultdef) then
  3397. tordconstnode(left).value:=tordconstnode(left).value and smallint($ffff)
  3398. else
  3399. tordconstnode(left).value:=tordconstnode(left).value and word($ffff);
  3400. 4:
  3401. if is_signed(resultdef) then
  3402. tordconstnode(left).value:=tordconstnode(left).value and longint($ffffffff)
  3403. else
  3404. tordconstnode(left).value:=tordconstnode(left).value and dword($ffffffff);
  3405. end;
  3406. end;
  3407. left.resultdef:=resultdef;
  3408. tordconstnode(left).typedef:=resultdef;
  3409. if is_signed(resultdef) then
  3410. tordconstnode(left).value.signed:=true
  3411. else
  3412. tordconstnode(left).value.signed:=false;
  3413. result:=left;
  3414. left:=nil;
  3415. exit;
  3416. end
  3417. else if (convtype=tc_int_2_int) and
  3418. is_currency(resultdef) then
  3419. begin
  3420. v:=tordconstnode(left).value;
  3421. if not(nf_internal in flags) and not(is_currency(left.resultdef)) then
  3422. v:=v*10000;
  3423. result:=cordconstnode.create(v,resultdef,false);
  3424. exit;
  3425. end;
  3426. end;
  3427. pointerconstn :
  3428. begin
  3429. { pointerconstn to any pointer is folded too }
  3430. if (resultdef.typ=pointerdef) then
  3431. begin
  3432. left.resultdef:=resultdef;
  3433. if ([nf_explicit,nf_internal] * flags <> []) then
  3434. include(left.flags, nf_explicit)
  3435. else
  3436. { no longer an ordconst with an explicit typecast }
  3437. exclude(left.flags, nf_explicit);
  3438. result:=left;
  3439. left:=nil;
  3440. exit;
  3441. end
  3442. { constant pointer to ordinal }
  3443. else if is_ordinal(resultdef) then
  3444. begin
  3445. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  3446. resultdef,not(nf_explicit in flags));
  3447. if ([nf_explicit,nf_internal] * flags <> []) then
  3448. include(hp.flags, nf_explicit);
  3449. result:=hp;
  3450. exit;
  3451. end;
  3452. end;
  3453. else
  3454. ;
  3455. end;
  3456. {$ifndef CPUNO32BITOPS}
  3457. { must be done before code below, because we need the
  3458. typeconversions for ordconstn's as well }
  3459. case convtype of
  3460. tc_bool_2_int,
  3461. tc_int_2_bool,
  3462. tc_int_2_int:
  3463. begin
  3464. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  3465. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  3466. begin
  3467. { avoid unnecessary widening of intermediary calculations
  3468. to 64 bit }
  3469. if (resultdef.size <= 4) and
  3470. is_64bitint(left.resultdef) and
  3471. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3472. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  3473. doremoveinttypeconvs(0,left,generrordef,not foundsint,s32inttype,u32inttype);
  3474. {$if defined(cpu16bitalu)}
  3475. if (resultdef.size <= 2) and
  3476. (is_32bitint(left.resultdef) or is_64bitint(left.resultdef)) and
  3477. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3478. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit],int64(low(smallint)),high(word)) then
  3479. doremoveinttypeconvs(0,left,generrordef,not foundsint,s16inttype,u16inttype);
  3480. {$endif defined(cpu16bitalu)}
  3481. {$if defined(cpu8bitalu)}
  3482. if (resultdef.size<left.resultdef.size) and
  3483. is_integer(left.resultdef) and
  3484. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3485. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  3486. doremoveinttypeconvs(0,left,generrordef,not foundsint,s8inttype,u8inttype);
  3487. {$endif defined(cpu8bitalu)}
  3488. { the above simplification may have left a redundant equal
  3489. typeconv (e.g. int32 to int32). If that's the case, we remove it }
  3490. if equal_defs(left.resultdef,resultdef) then
  3491. begin
  3492. result:=left;
  3493. left:=nil;
  3494. exit;
  3495. end;
  3496. if (convtype=tc_int_2_int) and (left.nodetype=typeconvn) and (ttypeconvnode(left).convtype=tc_bool_2_int) then
  3497. begin
  3498. ttypeconvnode(left).totypedef:=resultdef;
  3499. ttypeconvnode(left).resultdef:=resultdef;
  3500. result:=left;
  3501. left:=nil;
  3502. exit;
  3503. end;
  3504. end;
  3505. end;
  3506. else
  3507. ;
  3508. end;
  3509. {$endif not CPUNO32BITOPS}
  3510. end;
  3511. procedure Ttypeconvnode.mark_write;
  3512. begin
  3513. if not(convtype=tc_pointer_2_array) then
  3514. left.mark_write;
  3515. end;
  3516. function ttypeconvnode.first_cord_to_pointer : tnode;
  3517. begin
  3518. result:=nil;
  3519. internalerror(200104043);
  3520. end;
  3521. function ttypeconvnode.first_int_to_int : tnode;
  3522. begin
  3523. first_int_to_int:=nil;
  3524. expectloc:=left.expectloc;
  3525. if not is_void(left.resultdef) then
  3526. begin
  3527. if (left.expectloc<>LOC_REGISTER) and
  3528. ((resultdef.size>left.resultdef.size) or
  3529. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  3530. expectloc:=LOC_REGISTER
  3531. else
  3532. if (left.expectloc=LOC_CREGISTER) and
  3533. (resultdef.size<left.resultdef.size) then
  3534. expectloc:=LOC_REGISTER;
  3535. end;
  3536. end;
  3537. function ttypeconvnode.first_cstring_to_pchar : tnode;
  3538. begin
  3539. result:=nil;
  3540. expectloc:=LOC_REGISTER;
  3541. end;
  3542. function ttypeconvnode.first_cstring_to_int : tnode;
  3543. begin
  3544. result:=nil;
  3545. internalerror(200510014);
  3546. end;
  3547. function ttypeconvnode.first_string_to_chararray : tnode;
  3548. begin
  3549. first_string_to_chararray:=nil;
  3550. expectloc:=left.expectloc;
  3551. end;
  3552. function ttypeconvnode.first_char_to_string : tnode;
  3553. begin
  3554. first_char_to_string:=nil;
  3555. if tstringdef(resultdef).stringtype=st_shortstring then
  3556. inc(current_procinfo.estimatedtempsize,256);
  3557. expectloc:=LOC_REFERENCE;
  3558. end;
  3559. function ttypeconvnode.first_char_to_chararray : tnode;
  3560. begin
  3561. if resultdef.size <> 1 then
  3562. begin
  3563. { convert first to string, then to chararray }
  3564. inserttypeconv(left,cshortstringtype);
  3565. inserttypeconv(left,resultdef);
  3566. result:=left;
  3567. left := nil;
  3568. exit;
  3569. end;
  3570. result := nil;
  3571. end;
  3572. function ttypeconvnode.first_nothing : tnode;
  3573. begin
  3574. first_nothing:=nil;
  3575. end;
  3576. function ttypeconvnode.first_array_to_pointer : tnode;
  3577. begin
  3578. first_array_to_pointer:=nil;
  3579. make_not_regable(left,[ra_addr_regable]);
  3580. expectloc:=LOC_REGISTER;
  3581. end;
  3582. function ttypeconvnode.first_int_to_real: tnode;
  3583. var
  3584. fname: string[32];
  3585. begin
  3586. if target_info.system in systems_wince then
  3587. begin
  3588. { converting a 64bit integer to a float requires a helper }
  3589. if is_64bitint(left.resultdef) or
  3590. is_currency(left.resultdef) then
  3591. begin
  3592. { hack to avoid double division by 10000, as it's
  3593. already done by typecheckpass.resultdef_int_to_real }
  3594. if is_currency(left.resultdef) then
  3595. left.resultdef := s64inttype;
  3596. if is_signed(left.resultdef) then
  3597. fname:='i64to'
  3598. else
  3599. fname:='ui64to';
  3600. end
  3601. else
  3602. { other integers are supposed to be 32 bit }
  3603. begin
  3604. if is_signed(left.resultdef) then
  3605. fname:='ito'
  3606. else
  3607. fname:='uto';
  3608. firstpass(left);
  3609. end;
  3610. if tfloatdef(resultdef).floattype=s64real then
  3611. fname:=fname+'d'
  3612. else
  3613. fname:=fname+'s';
  3614. result:=ccallnode.createintern(fname,ccallparanode.create(
  3615. left,nil));
  3616. left:=nil;
  3617. firstpass(result);
  3618. exit;
  3619. end
  3620. else
  3621. begin
  3622. { converting a 64bit integer to a float requires a helper }
  3623. if is_64bitint(left.resultdef) or
  3624. is_currency(left.resultdef) then
  3625. begin
  3626. { hack to avoid double division by 10000, as it's
  3627. already done by typecheckpass.resultdef_int_to_real }
  3628. if is_currency(left.resultdef) then
  3629. left.resultdef := s64inttype;
  3630. if is_signed(left.resultdef) then
  3631. fname:='int64_to_'
  3632. else
  3633. { we can't do better currently }
  3634. fname:='qword_to_';
  3635. end
  3636. else
  3637. { other integers are supposed to be 32 bit }
  3638. begin
  3639. if is_signed(left.resultdef) then
  3640. fname:='int32_to_'
  3641. else
  3642. fname:='int64_to_';
  3643. firstpass(left);
  3644. end;
  3645. if tfloatdef(resultdef).floattype=s64real then
  3646. fname:=fname+'float64'
  3647. else
  3648. fname:=fname+'float32';
  3649. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  3650. left,nil)),resultdef);
  3651. left:=nil;
  3652. firstpass(result);
  3653. exit;
  3654. end;
  3655. end;
  3656. function ttypeconvnode.first_real_to_real : tnode;
  3657. begin
  3658. {$ifdef cpufpemu}
  3659. if cs_fp_emulation in current_settings.moduleswitches then
  3660. begin
  3661. if target_info.system in systems_wince then
  3662. begin
  3663. case tfloatdef(left.resultdef).floattype of
  3664. s32real:
  3665. case tfloatdef(resultdef).floattype of
  3666. s64real:
  3667. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  3668. s32real:
  3669. begin
  3670. result:=left;
  3671. left:=nil;
  3672. end;
  3673. else
  3674. internalerror(2005082704);
  3675. end;
  3676. s64real:
  3677. case tfloatdef(resultdef).floattype of
  3678. s32real:
  3679. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  3680. s64real:
  3681. begin
  3682. result:=left;
  3683. left:=nil;
  3684. end;
  3685. else
  3686. internalerror(2005082703);
  3687. end;
  3688. else
  3689. internalerror(2005082702);
  3690. end;
  3691. left:=nil;
  3692. firstpass(result);
  3693. exit;
  3694. end
  3695. else
  3696. begin
  3697. case tfloatdef(left.resultdef).floattype of
  3698. s32real:
  3699. case tfloatdef(resultdef).floattype of
  3700. s64real:
  3701. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  3702. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  3703. s32real:
  3704. begin
  3705. result:=left;
  3706. left:=nil;
  3707. end;
  3708. else
  3709. internalerror(200610151);
  3710. end;
  3711. s64real:
  3712. case tfloatdef(resultdef).floattype of
  3713. s32real:
  3714. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  3715. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  3716. s64real:
  3717. begin
  3718. result:=left;
  3719. left:=nil;
  3720. end;
  3721. else
  3722. internalerror(200610152);
  3723. end;
  3724. else
  3725. internalerror(200610153);
  3726. end;
  3727. left:=nil;
  3728. firstpass(result);
  3729. exit;
  3730. end;
  3731. end
  3732. else
  3733. {$endif cpufpemu}
  3734. begin
  3735. first_real_to_real:=nil;
  3736. if not use_vectorfpu(resultdef) then
  3737. expectloc:=LOC_FPUREGISTER
  3738. else
  3739. expectloc:=LOC_MMREGISTER;
  3740. end;
  3741. end;
  3742. function ttypeconvnode.first_pointer_to_array : tnode;
  3743. begin
  3744. first_pointer_to_array:=nil;
  3745. expectloc:=LOC_REFERENCE;
  3746. end;
  3747. function ttypeconvnode.first_cchar_to_pchar : tnode;
  3748. begin
  3749. first_cchar_to_pchar:=nil;
  3750. internalerror(200104021);
  3751. end;
  3752. function ttypeconvnode.first_bool_to_int : tnode;
  3753. begin
  3754. first_bool_to_int:=nil;
  3755. { byte(boolean) or word(wordbool) or longint(longbool) must
  3756. be accepted for var parameters }
  3757. if (nf_explicit in flags) and
  3758. (left.resultdef.size=resultdef.size) and
  3759. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3760. begin
  3761. expectloc:=left.expectloc;
  3762. exit;
  3763. end;
  3764. expectloc:=LOC_REGISTER;
  3765. end;
  3766. function ttypeconvnode.first_int_to_bool : tnode;
  3767. begin
  3768. first_int_to_bool:=nil;
  3769. { byte(boolean) or word(wordbool) or longint(longbool) must
  3770. be accepted for var parameters }
  3771. if (nf_explicit in flags) and
  3772. (left.resultdef.size=resultdef.size) and
  3773. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3774. begin
  3775. {$ifdef xtensa}
  3776. expectloc:=LOC_REGISTER;
  3777. {$else xtensa}
  3778. expectloc:=left.expectloc;
  3779. {$endif xtensa}
  3780. exit;
  3781. end;
  3782. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  3783. { convert to a boolean (only necessary for 32bit processors) }
  3784. { note: not if left is already a bool (qwordbool that is true, even if
  3785. only because the highest bit is set, must remain true if it is
  3786. --implicitly, unlike integers-- converted to another type of bool),
  3787. Left can already be a bool because this routine can also be called
  3788. from first_bool_to_bool }
  3789. if not is_boolean(left.resultdef) and
  3790. (left.resultdef.size > sizeof(aint)) and
  3791. (left.resultdef.size<>resultdef.size)
  3792. and is_cbool(resultdef) then
  3793. begin
  3794. left:=ctypeconvnode.create_internal(left,s32inttype);
  3795. firstpass(left);
  3796. exit;
  3797. end;
  3798. expectloc:=LOC_REGISTER;
  3799. end;
  3800. function ttypeconvnode.first_bool_to_bool : tnode;
  3801. begin
  3802. first_bool_to_bool:=nil;
  3803. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  3804. not is_cbool(resultdef) then
  3805. expectloc := left.expectloc
  3806. { the following cases use the code generation for bool_to_int/
  3807. int_to_bool -> also set their expectlocs }
  3808. else if (resultdef.size=left.resultdef.size) and
  3809. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  3810. result:=first_bool_to_int
  3811. else
  3812. result:=first_int_to_bool
  3813. end;
  3814. function ttypeconvnode.first_char_to_char : tnode;
  3815. var
  3816. fname: string[18];
  3817. begin
  3818. if (torddef(resultdef).ordtype=uchar) and
  3819. (torddef(left.resultdef).ordtype=uwidechar) then
  3820. fname := 'fpc_uchar_to_char'
  3821. else if (torddef(resultdef).ordtype=uwidechar) and
  3822. (torddef(left.resultdef).ordtype=uchar) then
  3823. fname := 'fpc_char_to_uchar'
  3824. else
  3825. internalerror(2007081201);
  3826. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  3827. left:=nil;
  3828. firstpass(result);
  3829. end;
  3830. function ttypeconvnode.first_proc_to_procvar : tnode;
  3831. begin
  3832. first_proc_to_procvar:=nil;
  3833. { if we take the address of a nested function, the current function/
  3834. procedure needs a stack frame since it's required to construct
  3835. the nested procvar }
  3836. if is_nested_pd(tprocvardef(resultdef)) and
  3837. (
  3838. not (po_anonymous in tprocdef(left.resultdef).procoptions) or
  3839. (po_delphi_nested_cc in tprocvardef(resultdef).procoptions)
  3840. ) then
  3841. include(current_procinfo.flags,pi_needs_stackframe);
  3842. if tabstractprocdef(resultdef).is_addressonly then
  3843. expectloc:=LOC_REGISTER
  3844. else
  3845. expectloc:=left.expectloc;
  3846. end;
  3847. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  3848. begin
  3849. first_nil_to_methodprocvar:=nil;
  3850. expectloc:=LOC_REGISTER;
  3851. end;
  3852. function ttypeconvnode.first_set_to_set : tnode;
  3853. var
  3854. newstatement : tstatementnode;
  3855. temp : ttempcreatenode;
  3856. begin
  3857. { in theory, we should do range checking here,
  3858. but Delphi doesn't do it either (FK) }
  3859. if left.nodetype=setconstn then
  3860. begin
  3861. left.resultdef:=resultdef;
  3862. result:=left;
  3863. left:=nil;
  3864. end
  3865. { equal sets for the code generator? }
  3866. else if (left.resultdef.size=resultdef.size) and
  3867. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  3868. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  3869. { e.g., this prints true because of this:
  3870. var
  3871. sa: set of 1..2;
  3872. sb: set of 5..6;
  3873. b: byte;
  3874. begin
  3875. b:=1;
  3876. sa:=[1..2];
  3877. sb:=sa;
  3878. writeln(b in sb);
  3879. end.
  3880. }
  3881. begin
  3882. result:=left;
  3883. left:=nil;
  3884. end
  3885. else
  3886. begin
  3887. result:=internalstatements(newstatement);
  3888. { in case left is a smallset expression, it can be an addn or so. }
  3889. { fpc_varset_load expects a formal const parameter, which doesn't }
  3890. { accept set addn's -> assign to a temp first and pass the temp }
  3891. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  3892. begin
  3893. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  3894. addstatement(newstatement,temp);
  3895. { temp := left }
  3896. addstatement(newstatement,cassignmentnode.create(
  3897. ctemprefnode.create(temp),left));
  3898. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3899. addstatement(newstatement,ctemprefnode.create(temp));
  3900. left:=result;
  3901. firstpass(left);
  3902. { recreate the result's internalstatements list }
  3903. result:=internalstatements(newstatement);
  3904. end;
  3905. { create temp for result }
  3906. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3907. addstatement(newstatement,temp);
  3908. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  3909. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  3910. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3911. ccallparanode.create(ctemprefnode.create(temp),
  3912. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3913. ccallparanode.create(left,nil))))))
  3914. );
  3915. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3916. addstatement(newstatement,ctemprefnode.create(temp));
  3917. left:=nil;
  3918. end;
  3919. end;
  3920. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  3921. begin
  3922. first_ansistring_to_pchar:=nil;
  3923. expectloc:=LOC_REGISTER;
  3924. end;
  3925. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  3926. begin
  3927. first_arrayconstructor_to_set:=nil;
  3928. internalerror(200104022);
  3929. end;
  3930. function ttypeconvnode.first_class_to_intf : tnode;
  3931. var
  3932. hd : tobjectdef;
  3933. ImplIntf : TImplementedInterface;
  3934. begin
  3935. result:=nil;
  3936. expectloc:=LOC_REGISTER;
  3937. hd:=tobjectdef(left.resultdef);
  3938. while assigned(hd) do
  3939. begin
  3940. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3941. if assigned(ImplIntf) then
  3942. begin
  3943. case ImplIntf.IType of
  3944. etStandard:
  3945. { handle in pass 2 }
  3946. ;
  3947. etFieldValue, etFieldValueClass:
  3948. if is_interface(tobjectdef(resultdef)) then
  3949. begin
  3950. result:=left;
  3951. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3952. { this ensures proper refcounting when field is of class type }
  3953. if not is_interface(result.resultdef) then
  3954. inserttypeconv(result, resultdef);
  3955. left:=nil;
  3956. end
  3957. else
  3958. begin
  3959. internalerror(200802213);
  3960. end;
  3961. etStaticMethodResult, etStaticMethodClass,
  3962. etVirtualMethodResult, etVirtualMethodClass:
  3963. if is_interface(tobjectdef(resultdef)) then
  3964. begin
  3965. { TODO: generating a call to TObject.GetInterface instead could yield
  3966. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3967. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3968. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3969. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3970. left,[],nil);
  3971. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3972. { if it is a class, process it further in a similar way }
  3973. if not is_interface(result.resultdef) then
  3974. inserttypeconv(result, resultdef);
  3975. left:=nil;
  3976. end
  3977. else if is_class(tobjectdef(resultdef)) then
  3978. begin
  3979. internalerror(200802211);
  3980. end
  3981. else
  3982. internalerror(200802231);
  3983. end;
  3984. break;
  3985. end;
  3986. hd:=hd.childof;
  3987. end;
  3988. if hd=nil then
  3989. internalerror(200802164);
  3990. end;
  3991. function ttypeconvnode.first_string_to_string : tnode;
  3992. var
  3993. procname: string[31];
  3994. newblock : tblocknode;
  3995. newstat : tstatementnode;
  3996. restemp : ttempcreatenode;
  3997. begin
  3998. { get the correct procedure name }
  3999. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  4000. '_to_'+tstringdef(resultdef).stringtypname;
  4001. if tstringdef(resultdef).stringtype=st_shortstring then
  4002. begin
  4003. newblock:=internalstatements(newstat);
  4004. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  4005. addstatement(newstat,restemp);
  4006. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  4007. ctemprefnode.create(restemp),nil))));
  4008. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  4009. addstatement(newstat,ctemprefnode.create(restemp));
  4010. result:=newblock;
  4011. end
  4012. { encoding parameter required? }
  4013. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  4014. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  4015. result:=ccallnode.createinternres(procname,
  4016. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  4017. ccallparanode.create(left,nil)),resultdef)
  4018. else
  4019. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  4020. left:=nil;
  4021. end;
  4022. function ttypeconvnode._first_int_to_int : tnode;
  4023. begin
  4024. result:=first_int_to_int;
  4025. end;
  4026. function ttypeconvnode._first_cstring_to_pchar : tnode;
  4027. begin
  4028. result:=first_cstring_to_pchar;
  4029. end;
  4030. function ttypeconvnode._first_cstring_to_int : tnode;
  4031. begin
  4032. result:=first_cstring_to_int;
  4033. end;
  4034. function ttypeconvnode._first_string_to_chararray : tnode;
  4035. begin
  4036. result:=first_string_to_chararray;
  4037. end;
  4038. function ttypeconvnode._first_char_to_string : tnode;
  4039. begin
  4040. result:=first_char_to_string;
  4041. end;
  4042. function ttypeconvnode._first_char_to_chararray: tnode;
  4043. begin
  4044. result:=first_char_to_chararray;
  4045. end;
  4046. function ttypeconvnode._first_nothing : tnode;
  4047. begin
  4048. result:=first_nothing;
  4049. end;
  4050. function ttypeconvnode._first_array_to_pointer : tnode;
  4051. begin
  4052. result:=first_array_to_pointer;
  4053. end;
  4054. function ttypeconvnode._first_int_to_real : tnode;
  4055. begin
  4056. result:=first_int_to_real;
  4057. end;
  4058. function ttypeconvnode._first_real_to_real : tnode;
  4059. begin
  4060. result:=first_real_to_real;
  4061. end;
  4062. function ttypeconvnode._first_pointer_to_array : tnode;
  4063. begin
  4064. result:=first_pointer_to_array;
  4065. end;
  4066. function ttypeconvnode._first_cchar_to_pchar : tnode;
  4067. begin
  4068. result:=first_cchar_to_pchar;
  4069. end;
  4070. function ttypeconvnode._first_bool_to_int : tnode;
  4071. begin
  4072. result:=first_bool_to_int;
  4073. end;
  4074. function ttypeconvnode._first_int_to_bool : tnode;
  4075. begin
  4076. result:=first_int_to_bool;
  4077. end;
  4078. function ttypeconvnode._first_bool_to_bool : tnode;
  4079. begin
  4080. result:=first_bool_to_bool;
  4081. end;
  4082. function ttypeconvnode._first_proc_to_procvar : tnode;
  4083. begin
  4084. result:=first_proc_to_procvar;
  4085. end;
  4086. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  4087. begin
  4088. result:=first_nil_to_methodprocvar;
  4089. end;
  4090. function ttypeconvnode._first_set_to_set : tnode;
  4091. begin
  4092. result:=first_set_to_set;
  4093. end;
  4094. function ttypeconvnode._first_cord_to_pointer : tnode;
  4095. begin
  4096. result:=first_cord_to_pointer;
  4097. end;
  4098. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  4099. begin
  4100. result:=first_ansistring_to_pchar;
  4101. end;
  4102. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  4103. begin
  4104. result:=first_arrayconstructor_to_set;
  4105. end;
  4106. function ttypeconvnode._first_class_to_intf : tnode;
  4107. begin
  4108. result:=first_class_to_intf;
  4109. end;
  4110. function ttypeconvnode._first_char_to_char : tnode;
  4111. begin
  4112. result:=first_char_to_char;
  4113. end;
  4114. function ttypeconvnode._first_string_to_string : tnode;
  4115. begin
  4116. result:=first_string_to_string;
  4117. end;
  4118. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  4119. const
  4120. firstconvert : array[tconverttype] of pointer = (
  4121. nil, { none }
  4122. @ttypeconvnode._first_nothing, {equal}
  4123. @ttypeconvnode._first_nothing, {not_possible}
  4124. @ttypeconvnode._first_string_to_string,
  4125. @ttypeconvnode._first_char_to_string,
  4126. @ttypeconvnode._first_char_to_chararray,
  4127. nil, { removed in typecheck_chararray_to_string }
  4128. @ttypeconvnode._first_cchar_to_pchar,
  4129. @ttypeconvnode._first_cstring_to_pchar,
  4130. @ttypeconvnode._first_cstring_to_int,
  4131. @ttypeconvnode._first_ansistring_to_pchar,
  4132. @ttypeconvnode._first_string_to_chararray,
  4133. nil, { removed in typecheck_chararray_to_string }
  4134. @ttypeconvnode._first_array_to_pointer,
  4135. @ttypeconvnode._first_pointer_to_array,
  4136. @ttypeconvnode._first_int_to_int,
  4137. @ttypeconvnode._first_int_to_bool,
  4138. @ttypeconvnode._first_bool_to_bool,
  4139. @ttypeconvnode._first_bool_to_int,
  4140. @ttypeconvnode._first_real_to_real,
  4141. @ttypeconvnode._first_int_to_real,
  4142. nil, { removed in typecheck_real_to_currency }
  4143. @ttypeconvnode._first_proc_to_procvar,
  4144. @ttypeconvnode._first_nil_to_methodprocvar,
  4145. @ttypeconvnode._first_arrayconstructor_to_set,
  4146. @ttypeconvnode._first_set_to_set,
  4147. @ttypeconvnode._first_cord_to_pointer,
  4148. @ttypeconvnode._first_nothing,
  4149. @ttypeconvnode._first_nothing,
  4150. @ttypeconvnode._first_class_to_intf,
  4151. @ttypeconvnode._first_char_to_char,
  4152. @ttypeconvnode._first_nothing,
  4153. @ttypeconvnode._first_nothing,
  4154. nil,
  4155. nil,
  4156. nil,
  4157. nil,
  4158. nil,
  4159. nil,
  4160. nil,
  4161. @ttypeconvnode._first_nothing,
  4162. @ttypeconvnode._first_nothing,
  4163. @ttypeconvnode._first_nothing,
  4164. nil,
  4165. nil
  4166. );
  4167. type
  4168. tprocedureofobject = function : tnode of object;
  4169. var
  4170. r : TMethod;
  4171. begin
  4172. { this is a little bit dirty but it works }
  4173. { and should be quite portable too }
  4174. r.Code:=firstconvert[c];
  4175. r.Data:=self;
  4176. if not assigned(r.Code) then
  4177. internalerror(200312081);
  4178. first_call_helper:=tprocedureofobject(r)()
  4179. end;
  4180. function ttypeconvnode.pass_1 : tnode;
  4181. begin
  4182. if warn_pointer_to_signed then
  4183. cgmessage(type_w_pointer_to_signed);
  4184. result:=nil;
  4185. firstpass(left);
  4186. if codegenerror then
  4187. exit;
  4188. expectloc:=left.expectloc;
  4189. if nf_explicit in flags then
  4190. { check if the result could be in a register }
  4191. if (not(tstoreddef(resultdef).is_intregable) and
  4192. not(tstoreddef(resultdef).is_const_intregable) and
  4193. not(tstoreddef(resultdef).is_fpuregable)) or
  4194. ((left.resultdef.typ = floatdef) and
  4195. (resultdef.typ <> floatdef)) then
  4196. make_not_regable(left,[ra_addr_regable]);
  4197. result:=first_call_helper(convtype);
  4198. end;
  4199. function ttypeconvnode.retains_value_location:boolean;
  4200. begin
  4201. result:=assigned(left.resultdef) and
  4202. (
  4203. (convtype=tc_equal) or
  4204. { typecasting from void is always allowed }
  4205. is_void(left.resultdef) or
  4206. (left.resultdef.typ=formaldef) or
  4207. { int 2 int with same size reuses same location, or for
  4208. tp7 mode also allow size < orignal size }
  4209. (
  4210. (convtype=tc_int_2_int) and
  4211. (
  4212. not is_bitpacked_access(left) and
  4213. (resultdef.size=left.resultdef.size) or
  4214. ((m_tp7 in current_settings.modeswitches) and
  4215. (resultdef.size<left.resultdef.size))
  4216. )
  4217. ) or
  4218. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  4219. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  4220. (nf_explicit in flags) and
  4221. (resultdef.size=left.resultdef.size)) or
  4222. { on managed platforms, converting an element to an open array
  4223. involves creating an actual array -> value location changes }
  4224. ((convtype=tc_elem_2_openarray) and
  4225. not(target_info.system in systems_managed_vm))
  4226. );
  4227. end;
  4228. function ttypeconvnode.assign_allowed:boolean;
  4229. begin
  4230. result:=retains_value_location;
  4231. { When using only a part of the value it can't be in a register since
  4232. that will load the value in a new register first }
  4233. { the same goes for changing the sign of equal-sized values which
  4234. are smaller than an entire register }
  4235. if result and
  4236. { don't try to check the size of an open array or an array of const }
  4237. (is_open_array(resultdef) or
  4238. is_open_array(left.resultdef) or
  4239. is_array_of_const(resultdef) or
  4240. is_array_of_const(left.resultdef) or
  4241. (resultdef.size<left.resultdef.size) or
  4242. ((resultdef.size=left.resultdef.size) and
  4243. (left.resultdef.size<sizeof(aint)) and
  4244. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  4245. make_not_regable(left,[ra_addr_regable]);
  4246. end;
  4247. function ttypeconvnode.docompare(p: tnode) : boolean;
  4248. begin
  4249. docompare :=
  4250. inherited docompare(p) and
  4251. (convtype = ttypeconvnode(p).convtype) and
  4252. (convnodeflags = ttypeconvnode(p).convnodeflags) and
  4253. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  4254. end;
  4255. procedure ttypeconvnode._second_int_to_int;
  4256. begin
  4257. second_int_to_int;
  4258. end;
  4259. procedure ttypeconvnode._second_string_to_string;
  4260. begin
  4261. second_string_to_string;
  4262. end;
  4263. procedure ttypeconvnode._second_cstring_to_pchar;
  4264. begin
  4265. second_cstring_to_pchar;
  4266. end;
  4267. procedure ttypeconvnode._second_cstring_to_int;
  4268. begin
  4269. second_cstring_to_int;
  4270. end;
  4271. procedure ttypeconvnode._second_string_to_chararray;
  4272. begin
  4273. second_string_to_chararray;
  4274. end;
  4275. procedure ttypeconvnode._second_array_to_pointer;
  4276. begin
  4277. second_array_to_pointer;
  4278. end;
  4279. procedure ttypeconvnode._second_pointer_to_array;
  4280. begin
  4281. second_pointer_to_array;
  4282. end;
  4283. procedure ttypeconvnode._second_chararray_to_string;
  4284. begin
  4285. second_chararray_to_string;
  4286. end;
  4287. procedure ttypeconvnode._second_char_to_string;
  4288. begin
  4289. second_char_to_string;
  4290. end;
  4291. procedure ttypeconvnode._second_int_to_real;
  4292. begin
  4293. second_int_to_real;
  4294. end;
  4295. procedure ttypeconvnode._second_real_to_real;
  4296. begin
  4297. second_real_to_real;
  4298. end;
  4299. procedure ttypeconvnode._second_cord_to_pointer;
  4300. begin
  4301. second_cord_to_pointer;
  4302. end;
  4303. procedure ttypeconvnode._second_proc_to_procvar;
  4304. begin
  4305. second_proc_to_procvar;
  4306. end;
  4307. procedure ttypeconvnode._second_nil_to_methodprocvar;
  4308. begin
  4309. second_nil_to_methodprocvar;
  4310. end;
  4311. procedure ttypeconvnode._second_bool_to_int;
  4312. begin
  4313. second_bool_to_int;
  4314. end;
  4315. procedure ttypeconvnode._second_int_to_bool;
  4316. begin
  4317. second_int_to_bool;
  4318. end;
  4319. procedure ttypeconvnode._second_bool_to_bool;
  4320. begin
  4321. second_bool_to_bool;
  4322. end;
  4323. procedure ttypeconvnode._second_set_to_set;
  4324. begin
  4325. second_set_to_set;
  4326. end;
  4327. procedure ttypeconvnode._second_ansistring_to_pchar;
  4328. begin
  4329. second_ansistring_to_pchar;
  4330. end;
  4331. procedure ttypeconvnode._second_class_to_intf;
  4332. begin
  4333. second_class_to_intf;
  4334. end;
  4335. procedure ttypeconvnode._second_char_to_char;
  4336. begin
  4337. second_char_to_char;
  4338. end;
  4339. procedure ttypeconvnode._second_elem_to_openarray;
  4340. begin
  4341. second_elem_to_openarray;
  4342. end;
  4343. procedure ttypeconvnode._second_nothing;
  4344. begin
  4345. second_nothing;
  4346. end;
  4347. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  4348. const
  4349. secondconvert : array[tconverttype] of pointer = (
  4350. @ttypeconvnode._second_nothing, {none}
  4351. @ttypeconvnode._second_nothing, {equal}
  4352. @ttypeconvnode._second_nothing, {not_possible}
  4353. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  4354. @ttypeconvnode._second_char_to_string,
  4355. @ttypeconvnode._second_nothing, {char_to_charray}
  4356. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  4357. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  4358. @ttypeconvnode._second_cstring_to_pchar,
  4359. @ttypeconvnode._second_cstring_to_int,
  4360. @ttypeconvnode._second_ansistring_to_pchar,
  4361. @ttypeconvnode._second_string_to_chararray,
  4362. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  4363. @ttypeconvnode._second_array_to_pointer,
  4364. @ttypeconvnode._second_pointer_to_array,
  4365. @ttypeconvnode._second_int_to_int,
  4366. @ttypeconvnode._second_int_to_bool,
  4367. @ttypeconvnode._second_bool_to_bool,
  4368. @ttypeconvnode._second_bool_to_int,
  4369. @ttypeconvnode._second_real_to_real,
  4370. @ttypeconvnode._second_int_to_real,
  4371. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  4372. @ttypeconvnode._second_proc_to_procvar,
  4373. @ttypeconvnode._second_nil_to_methodprocvar,
  4374. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  4375. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  4376. @ttypeconvnode._second_cord_to_pointer,
  4377. @ttypeconvnode._second_nothing, { interface 2 string }
  4378. @ttypeconvnode._second_nothing, { interface 2 guid }
  4379. @ttypeconvnode._second_class_to_intf,
  4380. @ttypeconvnode._second_char_to_char,
  4381. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  4382. @ttypeconvnode._second_nothing, { pwchar_2_string }
  4383. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  4384. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  4385. @ttypeconvnode._second_nothing, { variant_2_enum }
  4386. @ttypeconvnode._second_nothing, { enum_2_variant }
  4387. @ttypeconvnode._second_nothing, { variant_2_interface }
  4388. @ttypeconvnode._second_nothing, { interface_2_variant }
  4389. @ttypeconvnode._second_nothing, { array_2_dynarray }
  4390. @ttypeconvnode._second_elem_to_openarray, { elem_2_openarray }
  4391. @ttypeconvnode._second_nothing, { arrayconstructor_2_dynarray }
  4392. @ttypeconvnode._second_nothing, { arrayconstructor_2_array }
  4393. @ttypeconvnode._second_nothing, { anonproc_2_funcref }
  4394. @ttypeconvnode._second_nothing { procvar_2_funcref }
  4395. );
  4396. type
  4397. tprocedureofobject = procedure of object;
  4398. var
  4399. r : TMethod;
  4400. begin
  4401. { this is a little bit dirty but it works }
  4402. { and should be quite portable too }
  4403. r.Code:=secondconvert[c];
  4404. r.Data:=self;
  4405. tprocedureofobject(r)();
  4406. end;
  4407. {*****************************************************************************
  4408. TASNODE
  4409. *****************************************************************************}
  4410. function tasisnode.target_specific_typecheck: boolean;
  4411. begin
  4412. result:=false;
  4413. end;
  4414. function tasisnode.pass_typecheck: tnode;
  4415. var
  4416. hp : tnode;
  4417. begin
  4418. result:=nil;
  4419. typecheckpass(right);
  4420. typecheckpass(left);
  4421. set_varstate(right,vs_read,[vsf_must_be_valid]);
  4422. set_varstate(left,vs_read,[vsf_must_be_valid]);
  4423. if codegenerror then
  4424. exit;
  4425. if target_specific_typecheck then
  4426. begin
  4427. // ok
  4428. end
  4429. else if (right.resultdef.typ=classrefdef) then
  4430. begin
  4431. { left maybe an interface reference }
  4432. if is_interfacecom(left.resultdef) or
  4433. is_javainterface(left.resultdef) then
  4434. begin
  4435. { relation checks are not possible }
  4436. end
  4437. { or left must be a class }
  4438. else if is_class(left.resultdef) or
  4439. is_javaclass(left.resultdef) then
  4440. begin
  4441. { the operands must be related }
  4442. if (not(def_is_related(tobjectdef(left.resultdef),
  4443. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  4444. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  4445. tobjectdef(left.resultdef)))) then
  4446. CGMessage2(type_e_classes_not_related,
  4447. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  4448. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  4449. end
  4450. else
  4451. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  4452. case nodetype of
  4453. isn:
  4454. resultdef:=pasbool1type;
  4455. asn:
  4456. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  4457. else
  4458. ;
  4459. end;
  4460. end
  4461. else if is_interface(right.resultdef) or
  4462. is_dispinterface(right.resultdef) or
  4463. is_javainterface(right.resultdef) then
  4464. begin
  4465. case nodetype of
  4466. isn:
  4467. resultdef:=pasbool1type;
  4468. asn:
  4469. resultdef:=right.resultdef;
  4470. else
  4471. ;
  4472. end;
  4473. { left is a class or interface }
  4474. if is_javainterface(right.resultdef) then
  4475. begin
  4476. if not is_java_class_or_interface(left.resultdef) then
  4477. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  4478. end
  4479. else if not(is_class(left.resultdef) or
  4480. is_interfacecom(left.resultdef)) then
  4481. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  4482. else
  4483. begin
  4484. { load the GUID of the interface }
  4485. if (right.nodetype=typen) then
  4486. begin
  4487. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  4488. begin
  4489. if assigned(tobjectdef(right.resultdef).iidstr) then
  4490. begin
  4491. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  4492. tstringconstnode(hp).changestringtype(cshortstringtype);
  4493. right.free;
  4494. right:=hp;
  4495. end
  4496. else
  4497. internalerror(201006131);
  4498. end
  4499. else
  4500. begin
  4501. if assigned(tobjectdef(right.resultdef).iidguid) then
  4502. begin
  4503. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  4504. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  4505. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  4506. right.free;
  4507. right:=hp;
  4508. end
  4509. else
  4510. internalerror(201006132);
  4511. end;
  4512. typecheckpass(right);
  4513. end;
  4514. end;
  4515. end
  4516. else
  4517. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  4518. end;
  4519. {*****************************************************************************
  4520. TISNODE
  4521. *****************************************************************************}
  4522. constructor tisnode.create(l,r : tnode);
  4523. begin
  4524. inherited create(isn,l,r);
  4525. end;
  4526. constructor tisnode.create_internal(l, r: tnode);
  4527. begin
  4528. create(l,r);
  4529. include(flags,nf_internal);
  4530. end;
  4531. function tisnode.pass_1 : tnode;
  4532. var
  4533. procname: string;
  4534. statement : tstatementnode;
  4535. tempnode : ttempcreatenode;
  4536. begin
  4537. result:=nil;
  4538. { Passing a class type to an "is" expression cannot result in a class
  4539. of that type to be constructed.
  4540. }
  4541. include(right.flags,nf_ignore_for_wpo);
  4542. if is_class(left.resultdef) and
  4543. (right.resultdef.typ=classrefdef) then
  4544. begin
  4545. if (right.nodetype=loadvmtaddrn) and
  4546. (tloadvmtaddrnode(right).left.nodetype=typen) and
  4547. (oo_is_sealed in tobjectdef(tloadvmtaddrnode(right).left.resultdef).objectoptions) and
  4548. equal_defs(left.resultdef,tclassrefdef(right.resultdef).pointeddef) then
  4549. begin
  4550. if might_have_sideeffects(left) or
  4551. (node_complexity(left)>2) then
  4552. begin
  4553. result:=internalstatements(statement);
  4554. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  4555. addstatement(statement,tempnode);
  4556. addstatement(statement,cassignmentnode.create_internal(ctemprefnode.create(tempnode),left));
  4557. addstatement(statement,caddnode.create_internal(andn,
  4558. caddnode.create_internal(unequaln,ctemprefnode.create(tempnode),cnilnode.create),
  4559. caddnode.create_internal(equaln,cloadvmtaddrnode.create(ctemprefnode.create(tempnode)),right)
  4560. )
  4561. );
  4562. left:=nil;
  4563. right:=nil;
  4564. end
  4565. else
  4566. begin
  4567. result:=caddnode.create_internal(andn,
  4568. caddnode.create_internal(unequaln,left.getcopy,cnilnode.create),
  4569. caddnode.create_internal(equaln,cloadvmtaddrnode.create(left.getcopy),right)
  4570. );
  4571. right:=nil;
  4572. end;
  4573. end
  4574. else
  4575. result := ccallnode.createinternres('fpc_do_is',
  4576. ccallparanode.create(left,ccallparanode.create(right,nil)),
  4577. resultdef);
  4578. end
  4579. else
  4580. begin
  4581. if is_class(left.resultdef) then
  4582. if is_shortstring(right.resultdef) then
  4583. procname := 'fpc_class_is_corbaintf'
  4584. else
  4585. procname := 'fpc_class_is_intf'
  4586. else
  4587. if right.resultdef.typ=classrefdef then
  4588. procname := 'fpc_intf_is_class'
  4589. else
  4590. procname := 'fpc_intf_is';
  4591. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4592. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4593. end;
  4594. left := nil;
  4595. right := nil;
  4596. //firstpass(call);
  4597. if codegenerror then
  4598. exit;
  4599. end;
  4600. { dummy pass_2, it will never be called, but we need one since }
  4601. { you can't instantiate an abstract class }
  4602. procedure tisnode.pass_generate_code;
  4603. begin
  4604. end;
  4605. {*****************************************************************************
  4606. TASNODE
  4607. *****************************************************************************}
  4608. constructor tasnode.create(l,r : tnode);
  4609. begin
  4610. inherited create(asn,l,r);
  4611. call := nil;
  4612. end;
  4613. constructor tasnode.create_internal(l,r : tnode);
  4614. begin
  4615. create(l,r);
  4616. include(flags,nf_internal);
  4617. end;
  4618. destructor tasnode.destroy;
  4619. begin
  4620. call.free;
  4621. call := nil;
  4622. inherited destroy;
  4623. end;
  4624. function tasnode.dogetcopy: tnode;
  4625. begin
  4626. result := inherited dogetcopy;
  4627. if assigned(call) then
  4628. tasnode(result).call := call.getcopy
  4629. else
  4630. tasnode(result).call := nil;
  4631. end;
  4632. function tasnode.docompare(p: tnode): boolean;
  4633. begin
  4634. result:=
  4635. inherited docompare(p) and
  4636. tasnode(p).call.isequal(call);
  4637. end;
  4638. function tasnode.pass_1 : tnode;
  4639. var
  4640. procname: string;
  4641. begin
  4642. result:=nil;
  4643. { Passing a class type to an "as" expression cannot result in a class
  4644. of that type to be constructed.
  4645. We could put this inside the if-block below, but this way it is
  4646. safe for sure even if the code below changes
  4647. }
  4648. if assigned(right) then
  4649. include(right.flags,nf_ignore_for_wpo);
  4650. if not assigned(call) then
  4651. begin
  4652. if is_class(left.resultdef) and
  4653. (right.resultdef.typ=classrefdef) then
  4654. call := ccallnode.createinternres('fpc_do_as',
  4655. ccallparanode.create(left,ccallparanode.create(right,nil)),
  4656. resultdef)
  4657. else
  4658. begin
  4659. if is_class(left.resultdef) then
  4660. if is_shortstring(right.resultdef) then
  4661. procname := 'fpc_class_as_corbaintf'
  4662. else
  4663. procname := 'fpc_class_as_intf'
  4664. else
  4665. if right.resultdef.typ=classrefdef then
  4666. procname := 'fpc_intf_as_class'
  4667. else
  4668. procname := 'fpc_intf_as';
  4669. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4670. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4671. end;
  4672. left := nil;
  4673. right := nil;
  4674. firstpass(call);
  4675. if codegenerror then
  4676. exit;
  4677. expectloc:=call.expectloc;
  4678. end;
  4679. end;
  4680. end.