ncnv.pas 199 KB

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