ncnv.pas 172 KB

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