ncnv.pas 170 KB

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