ncnv.pas 173 KB

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