ncnv.pas 167 KB

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