ncnv.pas 175 KB

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