ncnv.pas 168 KB

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