ncnv.pas 203 KB

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