ncnv.pas 173 KB

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