ncnv.pas 174 KB

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