ncnv.pas 175 KB

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