ncnv.pas 173 KB

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