2
0

ncnv.pas 183 KB

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