ncnv.pas 172 KB

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