ncnv.pas 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385
  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,
  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(sinttype,sinttype.size,tt_persistent,false);
  1776. addstatement(newstatement,temp2);
  1777. { one dimensional }
  1778. addstatement(newstatement,cassignmentnode.create(
  1779. ctemprefnode.create(temp2),
  1780. cordconstnode.create
  1781. (paracount,s32inttype,true)));
  1782. { create call to fpc_dynarr_setlength }
  1783. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1784. ccallparanode.create(caddrnode.create_internal
  1785. (ctemprefnode.create(temp2)),
  1786. ccallparanode.create(cordconstnode.create
  1787. (1,s32inttype,true),
  1788. ccallparanode.create(caddrnode.create_internal
  1789. (crttinode.create(tstoreddef(totypedef),initrtti,rdt_normal)),
  1790. ccallparanode.create(
  1791. ctypeconvnode.create_internal(
  1792. ctemprefnode.create(arrnode),voidpointertype),
  1793. nil))))
  1794. ));
  1795. { add assignment statememnts }
  1796. addstatement(newstatement,ctempdeletenode.create(temp2));
  1797. if assigned(assnode) then
  1798. addstatement(newstatement,assnode);
  1799. { the last statement should return the value as
  1800. location and type, this is done be referencing the
  1801. temp and converting it first from a persistent temp to
  1802. normal temp }
  1803. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1804. addstatement(newstatement,ctemprefnode.create(arrnode));
  1805. end;
  1806. function ttypeconvnode._typecheck_int_to_int : tnode;
  1807. begin
  1808. result := typecheck_int_to_int;
  1809. end;
  1810. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  1811. begin
  1812. result := typecheck_cord_to_pointer;
  1813. end;
  1814. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  1815. begin
  1816. result := typecheck_chararray_to_string;
  1817. end;
  1818. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  1819. begin
  1820. result := typecheck_string_to_chararray;
  1821. end;
  1822. function ttypeconvnode._typecheck_string_to_string: tnode;
  1823. begin
  1824. result := typecheck_string_to_string;
  1825. end;
  1826. function ttypeconvnode._typecheck_char_to_string : tnode;
  1827. begin
  1828. result := typecheck_char_to_string;
  1829. end;
  1830. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  1831. begin
  1832. result := typecheck_char_to_chararray;
  1833. end;
  1834. function ttypeconvnode._typecheck_int_to_real : tnode;
  1835. begin
  1836. result := typecheck_int_to_real;
  1837. end;
  1838. function ttypeconvnode._typecheck_real_to_real : tnode;
  1839. begin
  1840. result := typecheck_real_to_real;
  1841. end;
  1842. function ttypeconvnode._typecheck_real_to_currency : tnode;
  1843. begin
  1844. result := typecheck_real_to_currency;
  1845. end;
  1846. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  1847. begin
  1848. result := typecheck_cchar_to_pchar;
  1849. end;
  1850. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  1851. begin
  1852. result := typecheck_cstring_to_pchar;
  1853. end;
  1854. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  1855. begin
  1856. result := typecheck_cstring_to_int;
  1857. end;
  1858. function ttypeconvnode._typecheck_char_to_char : tnode;
  1859. begin
  1860. result := typecheck_char_to_char;
  1861. end;
  1862. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  1863. begin
  1864. result := typecheck_arrayconstructor_to_set;
  1865. end;
  1866. function ttypeconvnode._typecheck_set_to_set : tnode;
  1867. begin
  1868. result := typecheck_set_to_set;
  1869. end;
  1870. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  1871. begin
  1872. result := typecheck_pchar_to_string;
  1873. end;
  1874. function ttypeconvnode._typecheck_interface_to_string : tnode;
  1875. begin
  1876. result := typecheck_interface_to_string;
  1877. end;
  1878. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  1879. begin
  1880. result := typecheck_interface_to_guid;
  1881. end;
  1882. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  1883. begin
  1884. result := typecheck_dynarray_to_openarray;
  1885. end;
  1886. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  1887. begin
  1888. result := typecheck_pwchar_to_string;
  1889. end;
  1890. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  1891. begin
  1892. result := typecheck_variant_to_dynarray;
  1893. end;
  1894. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  1895. begin
  1896. result := typecheck_dynarray_to_variant;
  1897. end;
  1898. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  1899. begin
  1900. result := typecheck_variant_to_enum;
  1901. end;
  1902. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  1903. begin
  1904. result := typecheck_enum_to_variant;
  1905. end;
  1906. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  1907. begin
  1908. result := typecheck_proc_to_procvar;
  1909. end;
  1910. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  1911. begin
  1912. result := typecheck_variant_to_interface;
  1913. end;
  1914. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  1915. begin
  1916. result := typecheck_interface_to_variant;
  1917. end;
  1918. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  1919. begin
  1920. result := typecheck_array_2_dynarray;
  1921. end;
  1922. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  1923. begin
  1924. result := typecheck_elem_2_openarray;
  1925. end;
  1926. function ttypeconvnode._typecheck_arrayconstructor_to_dynarray : tnode;
  1927. begin
  1928. result:=typecheck_arrayconstructor_to_dynarray;
  1929. end;
  1930. function ttypeconvnode.target_specific_general_typeconv: boolean;
  1931. begin
  1932. result:=false;
  1933. end;
  1934. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  1935. begin
  1936. result:=false;
  1937. end;
  1938. class function ttypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  1939. begin
  1940. result:=false;
  1941. end;
  1942. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1943. var
  1944. pd : tabstractprocdef;
  1945. copytype : tproccopytyp;
  1946. source: pnode;
  1947. begin
  1948. result:=nil;
  1949. pd:=tabstractprocdef(left.resultdef);
  1950. { create procvardef (default for create_proc_to_procvar is voiddef,
  1951. but if later a regular inserttypeconvnode() is used to insert a type
  1952. conversion to the actual procvardef, totypedef will be set to the
  1953. real procvartype that we are converting to) }
  1954. if assigned(totypedef) and
  1955. (totypedef.typ=procvardef) then
  1956. begin
  1957. { have to do this in typecheckpass so that it's triggered for
  1958. typed constant declarations }
  1959. if po_is_block in tprocvardef(totypedef).procoptions then
  1960. begin
  1961. { can only convert from procdef to procvardef, but in the mean
  1962. time other type conversions may have been inserted (pointers,
  1963. proc2procvar, ...) }
  1964. source:=actualtargetnode(@left);
  1965. while (source^.nodetype=typeconvn) and
  1966. (ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
  1967. (is_void(source^.resultdef) or
  1968. (source^.resultdef.typ=procvardef)) do
  1969. begin
  1970. { won't skip proc2procvar }
  1971. source:=actualtargetnode(@ttypeconvnode(source^).left);
  1972. end;
  1973. if (source^.nodetype=loadn) and
  1974. (source^.resultdef.typ=procdef) and
  1975. not is_nested_pd(tprocdef(source^.resultdef)) and
  1976. not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
  1977. begin
  1978. result:=generate_block_for_procaddr(tloadnode(source^));
  1979. exit;
  1980. end
  1981. else
  1982. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1983. end;
  1984. resultdef:=totypedef;
  1985. end
  1986. else
  1987. begin
  1988. { only need the address of the method? this is needed
  1989. for @tobject.create. In this case there will be a loadn without
  1990. a methodpointer. }
  1991. if (left.nodetype=loadn) and
  1992. not assigned(tloadnode(left).left) and
  1993. (not(m_nested_procvars in current_settings.modeswitches) or
  1994. not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
  1995. copytype:=pc_address_only
  1996. else
  1997. copytype:=pc_normal;
  1998. resultdef:=pd.getcopyas(procvardef,copytype);
  1999. end;
  2000. end;
  2001. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  2002. const
  2003. resultdefconvert : array[tconverttype] of pointer = (
  2004. {none} nil,
  2005. {equal} nil,
  2006. {not_possible} nil,
  2007. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  2008. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  2009. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  2010. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  2011. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  2012. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  2013. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  2014. { ansistring_2_pchar } nil,
  2015. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  2016. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  2017. { array_2_pointer } nil,
  2018. { pointer_2_array } nil,
  2019. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  2020. { int_2_bool } nil,
  2021. { bool_2_bool } nil,
  2022. { bool_2_int } nil,
  2023. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  2024. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  2025. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  2026. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  2027. { nil_2_methodprocvar } nil,
  2028. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  2029. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  2030. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  2031. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  2032. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  2033. { class_2_intf } nil,
  2034. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  2035. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  2036. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  2037. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  2038. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  2039. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  2040. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  2041. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  2042. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  2043. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  2044. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray,
  2045. { arrayconstructor_2_dynarray } @ttypeconvnode._typecheck_arrayconstructor_to_dynarray
  2046. );
  2047. type
  2048. tprocedureofobject = function : tnode of object;
  2049. var
  2050. r : packed record
  2051. proc : pointer;
  2052. obj : pointer;
  2053. end;
  2054. begin
  2055. result:=nil;
  2056. { this is a little bit dirty but it works }
  2057. { and should be quite portable too }
  2058. r.proc:=resultdefconvert[c];
  2059. r.obj:=self;
  2060. if assigned(r.proc) then
  2061. result:=tprocedureofobject(r)();
  2062. end;
  2063. function ttypeconvnode.pass_typecheck:tnode;
  2064. var
  2065. hdef : tdef;
  2066. hp : tnode;
  2067. currprocdef : tabstractprocdef;
  2068. aprocdef : tprocdef;
  2069. eq : tequaltype;
  2070. cdoptions : tcompare_defs_options;
  2071. newblock: tblocknode;
  2072. newstatement: tstatementnode;
  2073. tempnode: ttempcreatenode;
  2074. begin
  2075. result:=nil;
  2076. resultdef:=totypedef;
  2077. typecheckpass(left);
  2078. if codegenerror then
  2079. exit;
  2080. { When absolute force tc_equal }
  2081. if (nf_absolute in flags) then
  2082. begin
  2083. convtype:=tc_equal;
  2084. if (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  2085. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) then
  2086. make_not_regable(left,[ra_addr_regable]);
  2087. exit;
  2088. end;
  2089. { tp procvar support. Skip typecasts to procvar, record or set. Those
  2090. convert on the procvar value. This is used to access the
  2091. fields of a methodpointer }
  2092. if not(nf_load_procvar in flags) and
  2093. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  2094. maybe_call_procvar(left,true);
  2095. { convert array constructors to sets, because there is no conversion
  2096. possible for array constructors }
  2097. if (resultdef.typ<>arraydef) and
  2098. is_array_constructor(left.resultdef) then
  2099. begin
  2100. arrayconstructor_to_set(left);
  2101. typecheckpass(left);
  2102. end;
  2103. if target_specific_general_typeconv then
  2104. exit;
  2105. if convtype=tc_none then
  2106. begin
  2107. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  2108. { overloaded operators require calls, which is not possible inside
  2109. a constant declaration }
  2110. if (block_type<>bt_const) and
  2111. not(nf_internal in flags) then
  2112. include(cdoptions,cdo_check_operator);
  2113. if nf_explicit in flags then
  2114. include(cdoptions,cdo_explicit);
  2115. if nf_internal in flags then
  2116. include(cdoptions,cdo_internal);
  2117. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  2118. case eq of
  2119. te_exact,
  2120. te_equal :
  2121. begin
  2122. result := simplify(false);
  2123. if assigned(result) then
  2124. exit;
  2125. { in case of bitpacked accesses, the original type must
  2126. remain so that not too many/few bits are laoded }
  2127. if is_bitpacked_access(left) then
  2128. convtype:=tc_int_2_int;
  2129. { Only leave when there is no conversion to do.
  2130. We can still need to call a conversion routine,
  2131. like the routine to convert a stringconstnode }
  2132. if (convtype in [tc_equal,tc_not_possible]) and
  2133. { some conversions, like dynarray to pointer in Delphi
  2134. mode, must not be removed, because then we get memory
  2135. leaks due to missing temp finalization }
  2136. (not is_managed_type(left.resultdef) or
  2137. { different kinds of refcounted types may need calls
  2138. to different kinds of refcounting helpers }
  2139. (resultdef=left.resultdef)) then
  2140. begin
  2141. {$ifdef llvm}
  2142. { we still may have to insert a type conversion at the
  2143. llvm level }
  2144. if left.resultdef<>resultdef then
  2145. result:=nil
  2146. else
  2147. {$endif llvm}
  2148. begin
  2149. left.resultdef:=resultdef;
  2150. if (nf_explicit in flags) and (left.nodetype = addrn) then
  2151. include(left.flags, nf_typedaddr);
  2152. result:=left;
  2153. left:=nil;
  2154. end;
  2155. exit;
  2156. end;
  2157. end;
  2158. te_convert_l1,
  2159. te_convert_l2,
  2160. te_convert_l3,
  2161. te_convert_l4,
  2162. te_convert_l5,
  2163. te_convert_l6:
  2164. { nothing to do }
  2165. ;
  2166. te_convert_operator :
  2167. begin
  2168. include(current_procinfo.flags,pi_do_call);
  2169. addsymref(aprocdef.procsym);
  2170. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[],nil);
  2171. { tell explicitly which def we must use !! (PM) }
  2172. tcallnode(hp).procdefinition:=aprocdef;
  2173. left:=nil;
  2174. result:=hp;
  2175. exit;
  2176. end;
  2177. te_incompatible :
  2178. begin
  2179. { Procedures have a resultdef of voiddef and functions of their
  2180. own resultdef. They will therefore always be incompatible with
  2181. a procvar. Because isconvertable cannot check for procedures we
  2182. use an extra check for them.}
  2183. if (left.nodetype=calln) and
  2184. (tcallnode(left).required_para_count=0) and
  2185. (resultdef.typ=procvardef) and
  2186. (
  2187. (m_tp_procvar in current_settings.modeswitches) or
  2188. (m_mac_procvar in current_settings.modeswitches)
  2189. ) then
  2190. begin
  2191. if assigned(tcallnode(left).right) then
  2192. begin
  2193. { this is already a procvar, if it is really equal
  2194. is checked below }
  2195. convtype:=tc_equal;
  2196. hp:=tcallnode(left).right.getcopy;
  2197. currprocdef:=tabstractprocdef(hp.resultdef);
  2198. end
  2199. else
  2200. begin
  2201. convtype:=tc_proc_2_procvar;
  2202. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  2203. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  2204. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2205. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2206. begin
  2207. if assigned(tcallnode(left).methodpointer) then
  2208. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  2209. else
  2210. tloadnode(hp).set_mp(load_self_node);
  2211. end;
  2212. typecheckpass(hp);
  2213. end;
  2214. left.free;
  2215. left:=hp;
  2216. { Now check if the procedure we are going to assign to
  2217. the procvar, is compatible with the procvar's type }
  2218. if not(nf_explicit in flags) and
  2219. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
  2220. IncompatibleTypes(left.resultdef,resultdef)
  2221. else
  2222. result:=typecheck_call_helper(convtype);
  2223. exit;
  2224. end
  2225. else if maybe_global_proc_to_nested(left,resultdef) then
  2226. begin
  2227. result:=left;
  2228. left:=nil;
  2229. exit;
  2230. end;
  2231. { Handle explicit type conversions }
  2232. if nf_explicit in flags then
  2233. begin
  2234. { do common tc_equal cast, except when dealing with proc -> procvar
  2235. (may have to get rid of method pointer) }
  2236. if (left.resultdef.typ<>procdef) or
  2237. (resultdef.typ<>procvardef) then
  2238. convtype:=tc_equal
  2239. else
  2240. convtype:=tc_proc_2_procvar;
  2241. { ordinal constants can be resized to 1,2,4,8 bytes }
  2242. if (left.nodetype=ordconstn) then
  2243. begin
  2244. { Insert typeconv for ordinal to the correct size first on left, after
  2245. that the other conversion can be done }
  2246. hdef:=nil;
  2247. case longint(resultdef.size) of
  2248. 1 :
  2249. hdef:=s8inttype;
  2250. 2 :
  2251. hdef:=s16inttype;
  2252. 4 :
  2253. hdef:=s32inttype;
  2254. 8 :
  2255. hdef:=s64inttype;
  2256. end;
  2257. { we need explicit, because it can also be an enum }
  2258. if assigned(hdef) then
  2259. inserttypeconv_internal(left,hdef)
  2260. else
  2261. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2262. end;
  2263. { class/interface to class/interface, with checkobject support }
  2264. if is_class_or_interface_or_objc(resultdef) and
  2265. is_class_or_interface_or_objc(left.resultdef) then
  2266. begin
  2267. { check if the types are related }
  2268. if not(nf_internal in flags) and
  2269. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2270. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2271. begin
  2272. { Give an error when typecasting class to interface, this is compatible
  2273. with delphi }
  2274. if is_interface(resultdef) and
  2275. not is_interface(left.resultdef) then
  2276. CGMessage2(type_e_classes_not_related,
  2277. FullTypeName(left.resultdef,resultdef),
  2278. FullTypeName(resultdef,left.resultdef))
  2279. else
  2280. CGMessage2(type_w_classes_not_related,
  2281. FullTypeName(left.resultdef,resultdef),
  2282. FullTypeName(resultdef,left.resultdef))
  2283. end;
  2284. { Add runtime check? }
  2285. if not is_objc_class_or_protocol(resultdef) and
  2286. not is_objc_class_or_protocol(left.resultdef) and
  2287. (cs_check_object in current_settings.localswitches) and
  2288. not(nf_internal in flags) then
  2289. begin
  2290. { we can translate the typeconvnode to 'as' when
  2291. typecasting to a class or interface }
  2292. { we need to make sure the result can still be
  2293. passed as a var parameter }
  2294. newblock:=internalstatements(newstatement);
  2295. if (valid_for_var(left,false)) then
  2296. begin
  2297. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2298. addstatement(newstatement,tempnode);
  2299. addstatement(newstatement,cassignmentnode.create(
  2300. ctemprefnode.create(tempnode),
  2301. caddrnode.create_internal(left)));
  2302. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2303. end
  2304. else
  2305. begin
  2306. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2307. addstatement(newstatement,tempnode);
  2308. addstatement(newstatement,cassignmentnode.create(
  2309. ctemprefnode.create(tempnode),
  2310. left));
  2311. left:=ctemprefnode.create(tempnode);
  2312. end;
  2313. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2314. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2315. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2316. left:=nil;
  2317. result:=newblock;
  2318. exit;
  2319. end;
  2320. end
  2321. else
  2322. begin
  2323. { only if the same size or formal def, and }
  2324. { don't allow type casting of constants to }
  2325. { structured types }
  2326. if not(
  2327. (left.resultdef.typ=formaldef) or
  2328. {$ifdef jvm}
  2329. { enums /are/ class instances on the JVM
  2330. platform }
  2331. (((left.resultdef.typ=enumdef) and
  2332. (resultdef.typ=objectdef)) or
  2333. ((resultdef.typ=enumdef) and
  2334. (left.resultdef.typ=objectdef))) or
  2335. {$endif}
  2336. (
  2337. is_void(left.resultdef) and
  2338. (left.nodetype=derefn)
  2339. ) or
  2340. (
  2341. not(is_open_array(left.resultdef)) and
  2342. not(is_array_constructor(left.resultdef)) and
  2343. not(is_array_of_const(left.resultdef)) and
  2344. (left.resultdef.size=resultdef.size) and
  2345. { disallow casts of const nodes }
  2346. (not is_constnode(left) or
  2347. { however, there are some exceptions }
  2348. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2349. filedef,variantdef,objectdef]) or
  2350. is_class_or_interface_or_objc(resultdef) or
  2351. { the softfloat code generates casts <const. float> to record }
  2352. (nf_internal in flags)
  2353. ))
  2354. )
  2355. ) then
  2356. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2357. else
  2358. begin
  2359. { perform target-specific explicit typecast
  2360. checks }
  2361. if target_specific_explicit_typeconv then
  2362. begin
  2363. result:=simplify(false);
  2364. exit;
  2365. end;
  2366. end;
  2367. end;
  2368. end
  2369. else
  2370. IncompatibleTypes(left.resultdef,resultdef);
  2371. end;
  2372. else
  2373. internalerror(200211231);
  2374. end;
  2375. end;
  2376. { Give hint or warning for unportable code, exceptions are
  2377. - typecasts from constants
  2378. - void }
  2379. if not(nf_internal in flags) and
  2380. (left.nodetype<>ordconstn) and
  2381. not(is_void(left.resultdef)) and
  2382. (((left.resultdef.typ=orddef) and
  2383. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2384. ((resultdef.typ=orddef) and
  2385. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2386. begin
  2387. {Converting pointers to signed integers is a bad idea. Warn.}
  2388. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2389. { Give a warning when sizes don't match, because then info will be lost }
  2390. if left.resultdef.size=resultdef.size then
  2391. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2392. else
  2393. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2394. end;
  2395. { tc_cord_2_pointer still requires a type check, which
  2396. simplify does not do }
  2397. if (convtype<>tc_cord_2_pointer) then
  2398. begin
  2399. result := simplify(false);
  2400. if assigned(result) then
  2401. exit;
  2402. end;
  2403. { now call the resultdef helper to do constant folding }
  2404. result:=typecheck_call_helper(convtype);
  2405. end;
  2406. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2407. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2408. this applies is powerpc64
  2409. }
  2410. {$ifndef CPUNO32BITOPS}
  2411. { checks whether we can safely remove typeconversions to bigger types
  2412. in case range and overflow checking are off, and in case
  2413. the result of this node tree is downcasted again to a
  2414. smaller type value afterwards,
  2415. the smaller types being allowed are described by validints, ordinal constants must fit into l..h
  2416. We do this on 64 bit CPUs as well, they benefit from it as well }
  2417. function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;const l,h : Tconstexprint): boolean;
  2418. var
  2419. gotdivmod: boolean;
  2420. { checks whether a node has an accepted resultdef, or originally
  2421. had one but was implicitly converted to s64bit }
  2422. function wasoriginallysmallerint(n: tnode): boolean;
  2423. begin
  2424. if (n.resultdef.typ<>orddef) then
  2425. exit(false);
  2426. if (torddef(n.resultdef).ordtype in validints) then
  2427. begin
  2428. if is_signed(n.resultdef) then
  2429. gotsint:=true;
  2430. exit(true);
  2431. end;
  2432. { type conv to a bigger int, we do not like to use? }
  2433. if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
  2434. { nf_explicit is also set for explicitly typecasted }
  2435. { ordconstn's }
  2436. ([nf_internal,nf_explicit]*n.flags=[]) and
  2437. { either a typeconversion node coming from a smaller type }
  2438. (((n.nodetype=typeconvn) and
  2439. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2440. (torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
  2441. { or an ordconstnode which has a smaller type}
  2442. ((n.nodetype=ordconstn) and
  2443. (tordconstnode(n).value>=l) and
  2444. (tordconstnode(n).value<=h))) then
  2445. begin
  2446. if ((n.nodetype=typeconvn) and
  2447. is_signed(ttypeconvnode(n).left.resultdef)) or
  2448. ((n.nodetype=ordconstn) and
  2449. (tordconstnode(n).value<0)) then
  2450. gotsint:=true;
  2451. exit(true);
  2452. end;
  2453. result:=false;
  2454. end;
  2455. function docheckremoveinttypeconvs(n: tnode): boolean;
  2456. begin
  2457. result:=false;
  2458. if wasoriginallysmallerint(n) then
  2459. exit(true);
  2460. case n.nodetype of
  2461. subn,orn,xorn:
  2462. begin
  2463. { the result could become negative in this case }
  2464. if n.nodetype=subn then
  2465. gotsint:=true;
  2466. result:=
  2467. docheckremoveinttypeconvs(tbinarynode(n).left) and
  2468. docheckremoveinttypeconvs(tbinarynode(n).right);
  2469. end;
  2470. addn,muln,divn,modn,andn:
  2471. begin
  2472. if n.nodetype in [divn,modn] then
  2473. gotdivmod:=true;
  2474. result:=
  2475. (docheckremoveinttypeconvs(tbinarynode(n).left) and
  2476. docheckremoveinttypeconvs(tbinarynode(n).right)) or
  2477. { in case of div/mod, the result of that division/modulo can
  2478. usually be different in 32 and 64 bit }
  2479. (not gotdivmod and
  2480. (((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).left)) or
  2481. ((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).right))));
  2482. end;
  2483. end;
  2484. end;
  2485. begin { checkremove64bittypeconvs }
  2486. gotdivmod:=false;
  2487. gotsint:=false;
  2488. result:=
  2489. docheckremoveinttypeconvs(n) and
  2490. not(gotdivmod and gotsint);
  2491. end;
  2492. { remove int type conversions and set the result to the given type }
  2493. procedure doremoveinttypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
  2494. begin
  2495. case n.nodetype of
  2496. subn,addn,muln,divn,modn,xorn,andn,orn:
  2497. begin
  2498. exclude(n.flags,nf_internal);
  2499. if not forceunsigned and
  2500. is_signed(n.resultdef) then
  2501. begin
  2502. doremoveinttypeconvs(tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
  2503. doremoveinttypeconvs(tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
  2504. n.resultdef:=signedtype;
  2505. end
  2506. else
  2507. begin
  2508. doremoveinttypeconvs(tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  2509. doremoveinttypeconvs(tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
  2510. n.resultdef:=unsignedtype;
  2511. end;
  2512. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  2513. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  2514. // ) then
  2515. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  2516. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  2517. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  2518. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  2519. end;
  2520. typeconvn:
  2521. begin
  2522. ttypeconvnode(n).totypedef:=todef;
  2523. { may change the type conversion, e.g. if the old conversion was
  2524. from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
  2525. n.resultdef:=nil;
  2526. ttypeconvnode(n).convtype:=tc_none;
  2527. typecheckpass(n);
  2528. end;
  2529. else
  2530. inserttypeconv_internal(n,todef);
  2531. end;
  2532. end;
  2533. {$endif not CPUNO32BITOPS}
  2534. procedure swap_const_value (var val : TConstExprInt; size : longint);
  2535. begin
  2536. case size of
  2537. 1 : {do nothing };
  2538. 2 : if val.signed then
  2539. val.svalue:=swapendian(smallint(val.svalue))
  2540. else
  2541. val.uvalue:=swapendian(word(val.uvalue));
  2542. 4 : if val.signed then
  2543. val.svalue:=swapendian(longint(val.svalue))
  2544. else
  2545. val.uvalue:=swapendian(qword(val.uvalue));
  2546. 8 : if val.signed then
  2547. val.svalue:=swapendian(int64(val.svalue))
  2548. else
  2549. val.uvalue:=swapendian(qword(val.uvalue));
  2550. else
  2551. internalerror(2014111201);
  2552. end;
  2553. end;
  2554. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2555. var
  2556. hp: tnode;
  2557. {$ifndef CPUNO32BITOPS}
  2558. foundsint: boolean;
  2559. {$endif not CPUNO32BITOPS}
  2560. begin
  2561. result := nil;
  2562. { Constant folding and other node transitions to
  2563. remove the typeconv node }
  2564. case left.nodetype of
  2565. stringconstn :
  2566. if (resultdef.typ=stringdef) and
  2567. ((convtype=tc_equal) or
  2568. ((convtype=tc_string_2_string) and
  2569. (
  2570. ((not is_widechararray(left.resultdef) and
  2571. not is_wide_or_unicode_string(left.resultdef)) or
  2572. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  2573. )
  2574. )
  2575. )
  2576. ) then
  2577. begin
  2578. { output string consts in local ansistring encoding }
  2579. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0)or(tstringdef(resultdef).encoding=globals.CP_NONE)) then
  2580. tstringconstnode(left).changestringtype(getansistringdef)
  2581. else
  2582. tstringconstnode(left).changestringtype(resultdef);
  2583. result:=left;
  2584. resultdef:=left.resultdef;
  2585. left:=nil;
  2586. exit;
  2587. end;
  2588. realconstn :
  2589. begin
  2590. if (convtype = tc_real_2_currency) then
  2591. result := typecheck_real_to_currency
  2592. else if (convtype = tc_real_2_real) then
  2593. result := typecheck_real_to_real
  2594. else
  2595. exit;
  2596. if not(assigned(result)) then
  2597. begin
  2598. result := left;
  2599. left := nil;
  2600. end;
  2601. if (result.nodetype = realconstn) then
  2602. begin
  2603. hp:=result;
  2604. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2605. if ([nf_explicit,nf_internal] * flags <> []) then
  2606. include(result.flags, nf_explicit);
  2607. hp.free;
  2608. end;
  2609. end;
  2610. niln :
  2611. begin
  2612. { nil to ordinal node }
  2613. if (resultdef.typ=orddef) then
  2614. begin
  2615. hp:=cordconstnode.create(0,resultdef,true);
  2616. if ([nf_explicit,nf_internal] * flags <> []) then
  2617. include(hp.flags, nf_explicit);
  2618. result:=hp;
  2619. exit;
  2620. end
  2621. else
  2622. { fold nil to any pointer type }
  2623. if (resultdef.typ=pointerdef) then
  2624. begin
  2625. hp:=cnilnode.create;
  2626. hp.resultdef:=resultdef;
  2627. if ([nf_explicit,nf_internal] * flags <> []) then
  2628. include(hp.flags, nf_explicit);
  2629. result:=hp;
  2630. exit;
  2631. end
  2632. else
  2633. { remove typeconv after niln, but not when the result is a
  2634. methodpointer. The typeconv of the methodpointer will then
  2635. take care of updateing size of niln to OS_64 }
  2636. if not((resultdef.typ=procvardef) and
  2637. not(tprocvardef(resultdef).is_addressonly)) then
  2638. begin
  2639. left.resultdef:=resultdef;
  2640. if ([nf_explicit,nf_internal] * flags <> []) then
  2641. include(left.flags, nf_explicit);
  2642. result:=left;
  2643. left:=nil;
  2644. exit;
  2645. end;
  2646. end;
  2647. ordconstn :
  2648. begin
  2649. { ordinal contants can be directly converted }
  2650. { but not char to char because it is a widechar to char or via versa }
  2651. { which needs extra code to do the code page transistion }
  2652. { constant ordinal to pointer }
  2653. if (resultdef.typ=pointerdef) and
  2654. (convtype<>tc_cchar_2_pchar) then
  2655. begin
  2656. if (target_info.system in systems_managed_vm) and
  2657. (tordconstnode(left).value<>0) then
  2658. message(parser_e_feature_unsupported_for_vm);
  2659. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2660. if ([nf_explicit,nf_internal] * flags <> []) then
  2661. include(hp.flags, nf_explicit);
  2662. result:=hp;
  2663. exit;
  2664. end
  2665. else if is_ordinal(resultdef) and
  2666. not(convtype=tc_char_2_char) then
  2667. begin
  2668. { replace the resultdef and recheck the range }
  2669. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  2670. include(left.flags, nf_explicit)
  2671. else
  2672. { no longer an ordconst with an explicit typecast }
  2673. exclude(left.flags, nf_explicit);
  2674. { when converting from one boolean type to another, force }
  2675. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2676. { (Delphi-compatibile) }
  2677. if is_boolean(left.resultdef) and
  2678. is_boolean(resultdef) and
  2679. (is_cbool(left.resultdef) or
  2680. is_cbool(resultdef)) then
  2681. begin
  2682. if is_pasbool(resultdef) then
  2683. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2684. else
  2685. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2686. end
  2687. else
  2688. begin
  2689. { for constant values on absolute variables, swaping is required }
  2690. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2691. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  2692. if not(nf_internal in flags) then
  2693. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags)
  2694. or (nf_absolute in flags),false);
  2695. { swap value back, but according to new type }
  2696. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2697. swap_const_value(tordconstnode(left).value,resultdef.size);
  2698. { cut off the new value? }
  2699. if resultdef.size<left.resultdef.size then
  2700. case resultdef.size of
  2701. 1:
  2702. if is_signed(resultdef) then
  2703. tordconstnode(left).value:=tordconstnode(left).value and shortint($ff)
  2704. else
  2705. tordconstnode(left).value:=tordconstnode(left).value and byte($ff);
  2706. 2:
  2707. if is_signed(resultdef) then
  2708. tordconstnode(left).value:=tordconstnode(left).value and smallint($ffff)
  2709. else
  2710. tordconstnode(left).value:=tordconstnode(left).value and word($ffff);
  2711. 4:
  2712. if is_signed(resultdef) then
  2713. tordconstnode(left).value:=tordconstnode(left).value and longint($ffffffff)
  2714. else
  2715. tordconstnode(left).value:=tordconstnode(left).value and dword($ffffffff);
  2716. end;
  2717. end;
  2718. left.resultdef:=resultdef;
  2719. tordconstnode(left).typedef:=resultdef;
  2720. if is_signed(resultdef) then
  2721. tordconstnode(left).value.signed:=true
  2722. else
  2723. tordconstnode(left).value.signed:=false;
  2724. result:=left;
  2725. left:=nil;
  2726. exit;
  2727. end;
  2728. end;
  2729. pointerconstn :
  2730. begin
  2731. { pointerconstn to any pointer is folded too }
  2732. if (resultdef.typ=pointerdef) then
  2733. begin
  2734. left.resultdef:=resultdef;
  2735. if ([nf_explicit,nf_internal] * flags <> []) then
  2736. include(left.flags, nf_explicit)
  2737. else
  2738. { no longer an ordconst with an explicit typecast }
  2739. exclude(left.flags, nf_explicit);
  2740. result:=left;
  2741. left:=nil;
  2742. exit;
  2743. end
  2744. { constant pointer to ordinal }
  2745. else if is_ordinal(resultdef) then
  2746. begin
  2747. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2748. resultdef,not(nf_explicit in flags));
  2749. if ([nf_explicit,nf_internal] * flags <> []) then
  2750. include(hp.flags, nf_explicit);
  2751. result:=hp;
  2752. exit;
  2753. end;
  2754. end;
  2755. end;
  2756. {$ifndef CPUNO32BITOPS}
  2757. { must be done before code below, because we need the
  2758. typeconversions for ordconstn's as well }
  2759. case convtype of
  2760. tc_int_2_int:
  2761. begin
  2762. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2763. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  2764. begin
  2765. { avoid unnecessary widening of intermediary calculations
  2766. to 64 bit }
  2767. if (resultdef.size <= 4) and
  2768. is_64bitint(left.resultdef) and
  2769. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2770. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  2771. doremoveinttypeconvs(left,generrordef,not foundsint,s32inttype,u32inttype);
  2772. {$if defined(cpu16bitalu)}
  2773. if (resultdef.size <= 2) and
  2774. (is_32bitint(left.resultdef) or is_64bitint(left.resultdef)) and
  2775. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2776. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit],int64(low(smallint)),high(word)) then
  2777. doremoveinttypeconvs(left,generrordef,not foundsint,s16inttype,u16inttype);
  2778. {$endif defined(cpu16bitalu)}
  2779. {$if defined(cpu8bitalu)}
  2780. if (resultdef.size<left.resultdef.size) and
  2781. is_integer(left.resultdef) and
  2782. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2783. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  2784. doremoveinttypeconvs(left,generrordef,not foundsint,s8inttype,u8inttype);
  2785. {$endif defined(cpu8bitalu)}
  2786. end;
  2787. end;
  2788. end;
  2789. {$endif not CPUNO32BITOPS}
  2790. end;
  2791. procedure Ttypeconvnode.mark_write;
  2792. begin
  2793. left.mark_write;
  2794. end;
  2795. function ttypeconvnode.first_cord_to_pointer : tnode;
  2796. begin
  2797. result:=nil;
  2798. internalerror(200104043);
  2799. end;
  2800. function ttypeconvnode.first_int_to_int : tnode;
  2801. begin
  2802. first_int_to_int:=nil;
  2803. expectloc:=left.expectloc;
  2804. if not is_void(left.resultdef) then
  2805. begin
  2806. if (left.expectloc<>LOC_REGISTER) and
  2807. ((resultdef.size>left.resultdef.size) or
  2808. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2809. expectloc:=LOC_REGISTER
  2810. else
  2811. if (left.expectloc=LOC_CREGISTER) and
  2812. (resultdef.size<left.resultdef.size) then
  2813. expectloc:=LOC_REGISTER;
  2814. end;
  2815. end;
  2816. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2817. begin
  2818. result:=nil;
  2819. expectloc:=LOC_REGISTER;
  2820. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2821. if (cs_create_pic in current_settings.moduleswitches) then
  2822. include(current_procinfo.flags,pi_needs_got);
  2823. end;
  2824. function ttypeconvnode.first_cstring_to_int : tnode;
  2825. begin
  2826. result:=nil;
  2827. internalerror(200510014);
  2828. end;
  2829. function ttypeconvnode.first_string_to_chararray : tnode;
  2830. begin
  2831. first_string_to_chararray:=nil;
  2832. expectloc:=left.expectloc;
  2833. end;
  2834. function ttypeconvnode.first_char_to_string : tnode;
  2835. begin
  2836. first_char_to_string:=nil;
  2837. if tstringdef(resultdef).stringtype=st_shortstring then
  2838. inc(current_procinfo.estimatedtempsize,256);
  2839. expectloc:=LOC_REFERENCE;
  2840. end;
  2841. function ttypeconvnode.first_char_to_chararray : tnode;
  2842. begin
  2843. if resultdef.size <> 1 then
  2844. begin
  2845. { convert first to string, then to chararray }
  2846. inserttypeconv(left,cshortstringtype);
  2847. inserttypeconv(left,resultdef);
  2848. result:=left;
  2849. left := nil;
  2850. exit;
  2851. end;
  2852. result := nil;
  2853. end;
  2854. function ttypeconvnode.first_nothing : tnode;
  2855. begin
  2856. first_nothing:=nil;
  2857. end;
  2858. function ttypeconvnode.first_array_to_pointer : tnode;
  2859. begin
  2860. first_array_to_pointer:=nil;
  2861. expectloc:=LOC_REGISTER;
  2862. end;
  2863. function ttypeconvnode.first_int_to_real: tnode;
  2864. var
  2865. fname: string[32];
  2866. begin
  2867. if target_info.system in systems_wince then
  2868. begin
  2869. { converting a 64bit integer to a float requires a helper }
  2870. if is_64bitint(left.resultdef) or
  2871. is_currency(left.resultdef) then
  2872. begin
  2873. { hack to avoid double division by 10000, as it's
  2874. already done by typecheckpass.resultdef_int_to_real }
  2875. if is_currency(left.resultdef) then
  2876. left.resultdef := s64inttype;
  2877. if is_signed(left.resultdef) then
  2878. fname:='i64to'
  2879. else
  2880. fname:='ui64to';
  2881. end
  2882. else
  2883. { other integers are supposed to be 32 bit }
  2884. begin
  2885. if is_signed(left.resultdef) then
  2886. fname:='ito'
  2887. else
  2888. fname:='uto';
  2889. firstpass(left);
  2890. end;
  2891. if tfloatdef(resultdef).floattype=s64real then
  2892. fname:=fname+'d'
  2893. else
  2894. fname:=fname+'s';
  2895. result:=ccallnode.createintern(fname,ccallparanode.create(
  2896. left,nil));
  2897. left:=nil;
  2898. firstpass(result);
  2899. exit;
  2900. end
  2901. else
  2902. begin
  2903. { converting a 64bit integer to a float requires a helper }
  2904. if is_64bitint(left.resultdef) or
  2905. is_currency(left.resultdef) then
  2906. begin
  2907. { hack to avoid double division by 10000, as it's
  2908. already done by typecheckpass.resultdef_int_to_real }
  2909. if is_currency(left.resultdef) then
  2910. left.resultdef := s64inttype;
  2911. if is_signed(left.resultdef) then
  2912. fname:='int64_to_'
  2913. else
  2914. { we can't do better currently }
  2915. fname:='qword_to_';
  2916. end
  2917. else
  2918. { other integers are supposed to be 32 bit }
  2919. begin
  2920. if is_signed(left.resultdef) then
  2921. fname:='int32_to_'
  2922. else
  2923. fname:='int64_to_';
  2924. firstpass(left);
  2925. end;
  2926. if tfloatdef(resultdef).floattype=s64real then
  2927. fname:=fname+'float64'
  2928. else
  2929. fname:=fname+'float32';
  2930. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2931. left,nil)),resultdef);
  2932. left:=nil;
  2933. firstpass(result);
  2934. exit;
  2935. end;
  2936. end;
  2937. function ttypeconvnode.first_real_to_real : tnode;
  2938. begin
  2939. {$ifdef cpufpemu}
  2940. if cs_fp_emulation in current_settings.moduleswitches then
  2941. begin
  2942. if target_info.system in systems_wince then
  2943. begin
  2944. case tfloatdef(left.resultdef).floattype of
  2945. s32real:
  2946. case tfloatdef(resultdef).floattype of
  2947. s64real:
  2948. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  2949. s32real:
  2950. begin
  2951. result:=left;
  2952. left:=nil;
  2953. end;
  2954. else
  2955. internalerror(2005082704);
  2956. end;
  2957. s64real:
  2958. case tfloatdef(resultdef).floattype of
  2959. s32real:
  2960. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  2961. s64real:
  2962. begin
  2963. result:=left;
  2964. left:=nil;
  2965. end;
  2966. else
  2967. internalerror(2005082703);
  2968. end;
  2969. else
  2970. internalerror(2005082702);
  2971. end;
  2972. left:=nil;
  2973. firstpass(result);
  2974. exit;
  2975. end
  2976. else
  2977. begin
  2978. case tfloatdef(left.resultdef).floattype of
  2979. s32real:
  2980. case tfloatdef(resultdef).floattype of
  2981. s64real:
  2982. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2983. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2984. s32real:
  2985. begin
  2986. result:=left;
  2987. left:=nil;
  2988. end;
  2989. else
  2990. internalerror(200610151);
  2991. end;
  2992. s64real:
  2993. case tfloatdef(resultdef).floattype of
  2994. s32real:
  2995. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2996. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2997. s64real:
  2998. begin
  2999. result:=left;
  3000. left:=nil;
  3001. end;
  3002. else
  3003. internalerror(200610152);
  3004. end;
  3005. else
  3006. internalerror(200610153);
  3007. end;
  3008. left:=nil;
  3009. firstpass(result);
  3010. exit;
  3011. end;
  3012. end
  3013. else
  3014. {$endif cpufpemu}
  3015. begin
  3016. first_real_to_real:=nil;
  3017. if not use_vectorfpu(resultdef) then
  3018. expectloc:=LOC_FPUREGISTER
  3019. else
  3020. expectloc:=LOC_MMREGISTER;
  3021. end;
  3022. end;
  3023. function ttypeconvnode.first_pointer_to_array : tnode;
  3024. begin
  3025. first_pointer_to_array:=nil;
  3026. expectloc:=LOC_REFERENCE;
  3027. end;
  3028. function ttypeconvnode.first_cchar_to_pchar : tnode;
  3029. begin
  3030. first_cchar_to_pchar:=nil;
  3031. internalerror(200104021);
  3032. end;
  3033. function ttypeconvnode.first_bool_to_int : tnode;
  3034. begin
  3035. first_bool_to_int:=nil;
  3036. { byte(boolean) or word(wordbool) or longint(longbool) must
  3037. be accepted for var parameters }
  3038. if (nf_explicit in flags) and
  3039. (left.resultdef.size=resultdef.size) and
  3040. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3041. begin
  3042. expectloc:=left.expectloc;
  3043. exit;
  3044. end;
  3045. expectloc:=LOC_REGISTER;
  3046. end;
  3047. function ttypeconvnode.first_int_to_bool : tnode;
  3048. begin
  3049. first_int_to_bool:=nil;
  3050. { byte(boolean) or word(wordbool) or longint(longbool) must
  3051. be accepted for var parameters }
  3052. if (nf_explicit in flags) and
  3053. (left.resultdef.size=resultdef.size) and
  3054. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3055. begin
  3056. expectloc:=left.expectloc;
  3057. exit;
  3058. end;
  3059. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  3060. { convert to a boolean (only necessary for 32bit processors) }
  3061. { note: not if left is already a bool (qwordbool that is true, even if
  3062. only because the highest bit is set, must remain true if it is
  3063. --implicitly, unlike integers-- converted to another type of bool),
  3064. Left can already be a bool because this routine can also be called
  3065. from first_bool_to_bool }
  3066. if not is_boolean(left.resultdef) and
  3067. (left.resultdef.size > sizeof(aint)) and
  3068. (left.resultdef.size<>resultdef.size)
  3069. and is_cbool(resultdef) then
  3070. begin
  3071. left:=ctypeconvnode.create_internal(left,s32inttype);
  3072. firstpass(left);
  3073. exit;
  3074. end;
  3075. expectloc:=LOC_REGISTER;
  3076. end;
  3077. function ttypeconvnode.first_bool_to_bool : tnode;
  3078. begin
  3079. first_bool_to_bool:=nil;
  3080. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  3081. not is_cbool(resultdef) then
  3082. expectloc := left.expectloc
  3083. { the following cases use the code generation for bool_to_int/
  3084. int_to_bool -> also set their expectlocs }
  3085. else if (resultdef.size=left.resultdef.size) and
  3086. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  3087. result:=first_bool_to_int
  3088. else
  3089. result:=first_int_to_bool
  3090. end;
  3091. function ttypeconvnode.first_char_to_char : tnode;
  3092. var
  3093. fname: string[18];
  3094. begin
  3095. if (torddef(resultdef).ordtype=uchar) and
  3096. (torddef(left.resultdef).ordtype=uwidechar) then
  3097. fname := 'fpc_uchar_to_char'
  3098. else if (torddef(resultdef).ordtype=uwidechar) and
  3099. (torddef(left.resultdef).ordtype=uchar) then
  3100. fname := 'fpc_char_to_uchar'
  3101. else
  3102. internalerror(2007081201);
  3103. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  3104. left:=nil;
  3105. firstpass(result);
  3106. end;
  3107. function ttypeconvnode.first_proc_to_procvar : tnode;
  3108. begin
  3109. first_proc_to_procvar:=nil;
  3110. { if we take the address of a nested function, the current function/
  3111. procedure needs a stack frame since it's required to construct
  3112. the nested procvar }
  3113. if is_nested_pd(tprocvardef(resultdef)) then
  3114. include(current_procinfo.flags,pi_needs_stackframe);
  3115. if tabstractprocdef(resultdef).is_addressonly then
  3116. expectloc:=LOC_REGISTER
  3117. else
  3118. expectloc:=left.expectloc;
  3119. end;
  3120. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  3121. begin
  3122. first_nil_to_methodprocvar:=nil;
  3123. expectloc:=LOC_REGISTER;
  3124. end;
  3125. function ttypeconvnode.first_set_to_set : tnode;
  3126. var
  3127. newstatement : tstatementnode;
  3128. temp : ttempcreatenode;
  3129. begin
  3130. { in theory, we should do range checking here,
  3131. but Delphi doesn't do it either (FK) }
  3132. if left.nodetype=setconstn then
  3133. begin
  3134. left.resultdef:=resultdef;
  3135. result:=left;
  3136. left:=nil;
  3137. end
  3138. { equal sets for the code generator? }
  3139. else if (left.resultdef.size=resultdef.size) and
  3140. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  3141. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  3142. { e.g., this prints true because of this:
  3143. var
  3144. sa: set of 1..2;
  3145. sb: set of 5..6;
  3146. b: byte;
  3147. begin
  3148. b:=1;
  3149. sa:=[1..2];
  3150. sb:=sa;
  3151. writeln(b in sb);
  3152. end.
  3153. }
  3154. begin
  3155. result:=left;
  3156. left:=nil;
  3157. end
  3158. else
  3159. begin
  3160. result:=internalstatements(newstatement);
  3161. { in case left is a smallset expression, it can be an addn or so. }
  3162. { fpc_varset_load expects a formal const parameter, which doesn't }
  3163. { accept set addn's -> assign to a temp first and pass the temp }
  3164. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  3165. begin
  3166. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  3167. addstatement(newstatement,temp);
  3168. { temp := left }
  3169. addstatement(newstatement,cassignmentnode.create(
  3170. ctemprefnode.create(temp),left));
  3171. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3172. addstatement(newstatement,ctemprefnode.create(temp));
  3173. left:=result;
  3174. firstpass(left);
  3175. { recreate the result's internalstatements list }
  3176. result:=internalstatements(newstatement);
  3177. end;
  3178. { create temp for result }
  3179. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3180. addstatement(newstatement,temp);
  3181. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  3182. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  3183. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3184. ccallparanode.create(ctemprefnode.create(temp),
  3185. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3186. ccallparanode.create(left,nil))))))
  3187. );
  3188. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3189. addstatement(newstatement,ctemprefnode.create(temp));
  3190. left:=nil;
  3191. end;
  3192. end;
  3193. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  3194. begin
  3195. first_ansistring_to_pchar:=nil;
  3196. expectloc:=LOC_REGISTER;
  3197. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  3198. if (cs_create_pic in current_settings.moduleswitches) then
  3199. include(current_procinfo.flags,pi_needs_got);
  3200. end;
  3201. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  3202. begin
  3203. first_arrayconstructor_to_set:=nil;
  3204. internalerror(200104022);
  3205. end;
  3206. function ttypeconvnode.first_class_to_intf : tnode;
  3207. var
  3208. hd : tobjectdef;
  3209. ImplIntf : TImplementedInterface;
  3210. begin
  3211. result:=nil;
  3212. expectloc:=LOC_REGISTER;
  3213. hd:=tobjectdef(left.resultdef);
  3214. while assigned(hd) do
  3215. begin
  3216. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3217. if assigned(ImplIntf) then
  3218. begin
  3219. case ImplIntf.IType of
  3220. etStandard:
  3221. { handle in pass 2 }
  3222. ;
  3223. etFieldValue, etFieldValueClass:
  3224. if is_interface(tobjectdef(resultdef)) then
  3225. begin
  3226. result:=left;
  3227. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3228. { this ensures proper refcounting when field is of class type }
  3229. if not is_interface(result.resultdef) then
  3230. inserttypeconv(result, resultdef);
  3231. left:=nil;
  3232. end
  3233. else
  3234. begin
  3235. internalerror(200802213);
  3236. end;
  3237. etStaticMethodResult, etStaticMethodClass,
  3238. etVirtualMethodResult, etVirtualMethodClass:
  3239. if is_interface(tobjectdef(resultdef)) then
  3240. begin
  3241. { TODO: generating a call to TObject.GetInterface instead could yield
  3242. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3243. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3244. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3245. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3246. left,[],nil);
  3247. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3248. { if it is a class, process it further in a similar way }
  3249. if not is_interface(result.resultdef) then
  3250. inserttypeconv(result, resultdef);
  3251. left:=nil;
  3252. end
  3253. else if is_class(tobjectdef(resultdef)) then
  3254. begin
  3255. internalerror(200802211);
  3256. end
  3257. else
  3258. internalerror(200802231);
  3259. else
  3260. internalerror(200802165);
  3261. end;
  3262. break;
  3263. end;
  3264. hd:=hd.childof;
  3265. end;
  3266. if hd=nil then
  3267. internalerror(200802164);
  3268. end;
  3269. function ttypeconvnode.first_string_to_string : tnode;
  3270. var
  3271. procname: string[31];
  3272. newblock : tblocknode;
  3273. newstat : tstatementnode;
  3274. restemp : ttempcreatenode;
  3275. begin
  3276. { get the correct procedure name }
  3277. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3278. '_to_'+tstringdef(resultdef).stringtypname;
  3279. if tstringdef(resultdef).stringtype=st_shortstring then
  3280. begin
  3281. newblock:=internalstatements(newstat);
  3282. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3283. addstatement(newstat,restemp);
  3284. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3285. ctemprefnode.create(restemp),nil))));
  3286. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  3287. addstatement(newstat,ctemprefnode.create(restemp));
  3288. result:=newblock;
  3289. end
  3290. { encoding parameter required? }
  3291. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  3292. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  3293. result:=ccallnode.createinternres(procname,
  3294. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  3295. ccallparanode.create(left,nil)),resultdef)
  3296. else
  3297. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  3298. left:=nil;
  3299. end;
  3300. function ttypeconvnode._first_int_to_int : tnode;
  3301. begin
  3302. result:=first_int_to_int;
  3303. end;
  3304. function ttypeconvnode._first_cstring_to_pchar : tnode;
  3305. begin
  3306. result:=first_cstring_to_pchar;
  3307. end;
  3308. function ttypeconvnode._first_cstring_to_int : tnode;
  3309. begin
  3310. result:=first_cstring_to_int;
  3311. end;
  3312. function ttypeconvnode._first_string_to_chararray : tnode;
  3313. begin
  3314. result:=first_string_to_chararray;
  3315. end;
  3316. function ttypeconvnode._first_char_to_string : tnode;
  3317. begin
  3318. result:=first_char_to_string;
  3319. end;
  3320. function ttypeconvnode._first_char_to_chararray: tnode;
  3321. begin
  3322. result:=first_char_to_chararray;
  3323. end;
  3324. function ttypeconvnode._first_nothing : tnode;
  3325. begin
  3326. result:=first_nothing;
  3327. end;
  3328. function ttypeconvnode._first_array_to_pointer : tnode;
  3329. begin
  3330. result:=first_array_to_pointer;
  3331. end;
  3332. function ttypeconvnode._first_int_to_real : tnode;
  3333. begin
  3334. result:=first_int_to_real;
  3335. end;
  3336. function ttypeconvnode._first_real_to_real : tnode;
  3337. begin
  3338. result:=first_real_to_real;
  3339. end;
  3340. function ttypeconvnode._first_pointer_to_array : tnode;
  3341. begin
  3342. result:=first_pointer_to_array;
  3343. end;
  3344. function ttypeconvnode._first_cchar_to_pchar : tnode;
  3345. begin
  3346. result:=first_cchar_to_pchar;
  3347. end;
  3348. function ttypeconvnode._first_bool_to_int : tnode;
  3349. begin
  3350. result:=first_bool_to_int;
  3351. end;
  3352. function ttypeconvnode._first_int_to_bool : tnode;
  3353. begin
  3354. result:=first_int_to_bool;
  3355. end;
  3356. function ttypeconvnode._first_bool_to_bool : tnode;
  3357. begin
  3358. result:=first_bool_to_bool;
  3359. end;
  3360. function ttypeconvnode._first_proc_to_procvar : tnode;
  3361. begin
  3362. result:=first_proc_to_procvar;
  3363. end;
  3364. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  3365. begin
  3366. result:=first_nil_to_methodprocvar;
  3367. end;
  3368. function ttypeconvnode._first_set_to_set : tnode;
  3369. begin
  3370. result:=first_set_to_set;
  3371. end;
  3372. function ttypeconvnode._first_cord_to_pointer : tnode;
  3373. begin
  3374. result:=first_cord_to_pointer;
  3375. end;
  3376. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  3377. begin
  3378. result:=first_ansistring_to_pchar;
  3379. end;
  3380. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  3381. begin
  3382. result:=first_arrayconstructor_to_set;
  3383. end;
  3384. function ttypeconvnode._first_class_to_intf : tnode;
  3385. begin
  3386. result:=first_class_to_intf;
  3387. end;
  3388. function ttypeconvnode._first_char_to_char : tnode;
  3389. begin
  3390. result:=first_char_to_char;
  3391. end;
  3392. function ttypeconvnode._first_string_to_string : tnode;
  3393. begin
  3394. result:=first_string_to_string;
  3395. end;
  3396. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  3397. const
  3398. firstconvert : array[tconverttype] of pointer = (
  3399. nil, { none }
  3400. @ttypeconvnode._first_nothing, {equal}
  3401. @ttypeconvnode._first_nothing, {not_possible}
  3402. @ttypeconvnode._first_string_to_string,
  3403. @ttypeconvnode._first_char_to_string,
  3404. @ttypeconvnode._first_char_to_chararray,
  3405. nil, { removed in typecheck_chararray_to_string }
  3406. @ttypeconvnode._first_cchar_to_pchar,
  3407. @ttypeconvnode._first_cstring_to_pchar,
  3408. @ttypeconvnode._first_cstring_to_int,
  3409. @ttypeconvnode._first_ansistring_to_pchar,
  3410. @ttypeconvnode._first_string_to_chararray,
  3411. nil, { removed in typecheck_chararray_to_string }
  3412. @ttypeconvnode._first_array_to_pointer,
  3413. @ttypeconvnode._first_pointer_to_array,
  3414. @ttypeconvnode._first_int_to_int,
  3415. @ttypeconvnode._first_int_to_bool,
  3416. @ttypeconvnode._first_bool_to_bool,
  3417. @ttypeconvnode._first_bool_to_int,
  3418. @ttypeconvnode._first_real_to_real,
  3419. @ttypeconvnode._first_int_to_real,
  3420. nil, { removed in typecheck_real_to_currency }
  3421. @ttypeconvnode._first_proc_to_procvar,
  3422. @ttypeconvnode._first_nil_to_methodprocvar,
  3423. @ttypeconvnode._first_arrayconstructor_to_set,
  3424. @ttypeconvnode._first_set_to_set,
  3425. @ttypeconvnode._first_cord_to_pointer,
  3426. @ttypeconvnode._first_nothing,
  3427. @ttypeconvnode._first_nothing,
  3428. @ttypeconvnode._first_class_to_intf,
  3429. @ttypeconvnode._first_char_to_char,
  3430. @ttypeconvnode._first_nothing,
  3431. @ttypeconvnode._first_nothing,
  3432. nil,
  3433. nil,
  3434. nil,
  3435. nil,
  3436. nil,
  3437. nil,
  3438. nil,
  3439. @ttypeconvnode._first_nothing,
  3440. @ttypeconvnode._first_nothing
  3441. );
  3442. type
  3443. tprocedureofobject = function : tnode of object;
  3444. var
  3445. r : packed record
  3446. proc : pointer;
  3447. obj : pointer;
  3448. end;
  3449. begin
  3450. { this is a little bit dirty but it works }
  3451. { and should be quite portable too }
  3452. r.proc:=firstconvert[c];
  3453. r.obj:=self;
  3454. if not assigned(r.proc) then
  3455. internalerror(200312081);
  3456. first_call_helper:=tprocedureofobject(r)()
  3457. end;
  3458. function ttypeconvnode.pass_1 : tnode;
  3459. begin
  3460. if warn_pointer_to_signed then
  3461. cgmessage(type_w_pointer_to_signed);
  3462. result:=nil;
  3463. firstpass(left);
  3464. if codegenerror then
  3465. exit;
  3466. expectloc:=left.expectloc;
  3467. if nf_explicit in flags then
  3468. { check if the result could be in a register }
  3469. if (not(tstoreddef(resultdef).is_intregable) and
  3470. not(tstoreddef(resultdef).is_const_intregable) and
  3471. not(tstoreddef(resultdef).is_fpuregable)) or
  3472. ((left.resultdef.typ = floatdef) and
  3473. (resultdef.typ <> floatdef)) then
  3474. make_not_regable(left,[ra_addr_regable]);
  3475. result:=first_call_helper(convtype);
  3476. end;
  3477. function ttypeconvnode.retains_value_location:boolean;
  3478. begin
  3479. result:=(convtype=tc_equal) or
  3480. { typecasting from void is always allowed }
  3481. is_void(left.resultdef) or
  3482. (left.resultdef.typ=formaldef) or
  3483. { int 2 int with same size reuses same location, or for
  3484. tp7 mode also allow size < orignal size }
  3485. (
  3486. (convtype=tc_int_2_int) and
  3487. (
  3488. not is_bitpacked_access(left) and
  3489. (resultdef.size=left.resultdef.size) or
  3490. ((m_tp7 in current_settings.modeswitches) and
  3491. (resultdef.size<left.resultdef.size))
  3492. )
  3493. ) or
  3494. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  3495. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  3496. (nf_explicit in flags) and
  3497. (resultdef.size=left.resultdef.size)) or
  3498. { on managed platforms, converting an element to an open array
  3499. involves creating an actual array -> value location changes }
  3500. ((convtype=tc_elem_2_openarray) and
  3501. not(target_info.system in systems_managed_vm));
  3502. end;
  3503. function ttypeconvnode.assign_allowed:boolean;
  3504. begin
  3505. result:=retains_value_location;
  3506. { When using only a part of the value it can't be in a register since
  3507. that will load the value in a new register first }
  3508. { the same goes for changing the sign of equal-sized values which
  3509. are smaller than an entire register }
  3510. if result and
  3511. { don't try to check the size of an open array }
  3512. (is_open_array(resultdef) or
  3513. (resultdef.size<left.resultdef.size) or
  3514. ((resultdef.size=left.resultdef.size) and
  3515. (left.resultdef.size<sizeof(aint)) and
  3516. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  3517. make_not_regable(left,[ra_addr_regable]);
  3518. end;
  3519. function ttypeconvnode.docompare(p: tnode) : boolean;
  3520. begin
  3521. docompare :=
  3522. inherited docompare(p) and
  3523. (convtype = ttypeconvnode(p).convtype) and
  3524. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  3525. end;
  3526. procedure ttypeconvnode._second_int_to_int;
  3527. begin
  3528. second_int_to_int;
  3529. end;
  3530. procedure ttypeconvnode._second_string_to_string;
  3531. begin
  3532. second_string_to_string;
  3533. end;
  3534. procedure ttypeconvnode._second_cstring_to_pchar;
  3535. begin
  3536. second_cstring_to_pchar;
  3537. end;
  3538. procedure ttypeconvnode._second_cstring_to_int;
  3539. begin
  3540. second_cstring_to_int;
  3541. end;
  3542. procedure ttypeconvnode._second_string_to_chararray;
  3543. begin
  3544. second_string_to_chararray;
  3545. end;
  3546. procedure ttypeconvnode._second_array_to_pointer;
  3547. begin
  3548. second_array_to_pointer;
  3549. end;
  3550. procedure ttypeconvnode._second_pointer_to_array;
  3551. begin
  3552. second_pointer_to_array;
  3553. end;
  3554. procedure ttypeconvnode._second_chararray_to_string;
  3555. begin
  3556. second_chararray_to_string;
  3557. end;
  3558. procedure ttypeconvnode._second_char_to_string;
  3559. begin
  3560. second_char_to_string;
  3561. end;
  3562. procedure ttypeconvnode._second_int_to_real;
  3563. begin
  3564. second_int_to_real;
  3565. end;
  3566. procedure ttypeconvnode._second_real_to_real;
  3567. begin
  3568. second_real_to_real;
  3569. end;
  3570. procedure ttypeconvnode._second_cord_to_pointer;
  3571. begin
  3572. second_cord_to_pointer;
  3573. end;
  3574. procedure ttypeconvnode._second_proc_to_procvar;
  3575. begin
  3576. second_proc_to_procvar;
  3577. end;
  3578. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3579. begin
  3580. second_nil_to_methodprocvar;
  3581. end;
  3582. procedure ttypeconvnode._second_bool_to_int;
  3583. begin
  3584. second_bool_to_int;
  3585. end;
  3586. procedure ttypeconvnode._second_int_to_bool;
  3587. begin
  3588. second_int_to_bool;
  3589. end;
  3590. procedure ttypeconvnode._second_bool_to_bool;
  3591. begin
  3592. second_bool_to_bool;
  3593. end;
  3594. procedure ttypeconvnode._second_set_to_set;
  3595. begin
  3596. second_set_to_set;
  3597. end;
  3598. procedure ttypeconvnode._second_ansistring_to_pchar;
  3599. begin
  3600. second_ansistring_to_pchar;
  3601. end;
  3602. procedure ttypeconvnode._second_class_to_intf;
  3603. begin
  3604. second_class_to_intf;
  3605. end;
  3606. procedure ttypeconvnode._second_char_to_char;
  3607. begin
  3608. second_char_to_char;
  3609. end;
  3610. procedure ttypeconvnode._second_elem_to_openarray;
  3611. begin
  3612. second_elem_to_openarray;
  3613. end;
  3614. procedure ttypeconvnode._second_nothing;
  3615. begin
  3616. second_nothing;
  3617. end;
  3618. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3619. const
  3620. secondconvert : array[tconverttype] of pointer = (
  3621. @ttypeconvnode._second_nothing, {none}
  3622. @ttypeconvnode._second_nothing, {equal}
  3623. @ttypeconvnode._second_nothing, {not_possible}
  3624. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3625. @ttypeconvnode._second_char_to_string,
  3626. @ttypeconvnode._second_nothing, {char_to_charray}
  3627. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3628. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3629. @ttypeconvnode._second_cstring_to_pchar,
  3630. @ttypeconvnode._second_cstring_to_int,
  3631. @ttypeconvnode._second_ansistring_to_pchar,
  3632. @ttypeconvnode._second_string_to_chararray,
  3633. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3634. @ttypeconvnode._second_array_to_pointer,
  3635. @ttypeconvnode._second_pointer_to_array,
  3636. @ttypeconvnode._second_int_to_int,
  3637. @ttypeconvnode._second_int_to_bool,
  3638. @ttypeconvnode._second_bool_to_bool,
  3639. @ttypeconvnode._second_bool_to_int,
  3640. @ttypeconvnode._second_real_to_real,
  3641. @ttypeconvnode._second_int_to_real,
  3642. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3643. @ttypeconvnode._second_proc_to_procvar,
  3644. @ttypeconvnode._second_nil_to_methodprocvar,
  3645. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3646. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3647. @ttypeconvnode._second_cord_to_pointer,
  3648. @ttypeconvnode._second_nothing, { interface 2 string }
  3649. @ttypeconvnode._second_nothing, { interface 2 guid }
  3650. @ttypeconvnode._second_class_to_intf,
  3651. @ttypeconvnode._second_char_to_char,
  3652. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3653. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3654. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3655. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3656. @ttypeconvnode._second_nothing, { variant_2_enum }
  3657. @ttypeconvnode._second_nothing, { enum_2_variant }
  3658. @ttypeconvnode._second_nothing, { variant_2_interface }
  3659. @ttypeconvnode._second_nothing, { interface_2_variant }
  3660. @ttypeconvnode._second_nothing, { array_2_dynarray }
  3661. @ttypeconvnode._second_elem_to_openarray, { elem_2_openarray }
  3662. @ttypeconvnode._second_nothing { arrayconstructor_2_dynarray }
  3663. );
  3664. type
  3665. tprocedureofobject = procedure of object;
  3666. var
  3667. r : packed record
  3668. proc : pointer;
  3669. obj : pointer;
  3670. end;
  3671. begin
  3672. { this is a little bit dirty but it works }
  3673. { and should be quite portable too }
  3674. r.proc:=secondconvert[c];
  3675. r.obj:=self;
  3676. tprocedureofobject(r)();
  3677. end;
  3678. {*****************************************************************************
  3679. TASNODE
  3680. *****************************************************************************}
  3681. function tasisnode.target_specific_typecheck: boolean;
  3682. begin
  3683. result:=false;
  3684. end;
  3685. function tasisnode.pass_typecheck: tnode;
  3686. var
  3687. hp : tnode;
  3688. begin
  3689. result:=nil;
  3690. typecheckpass(right);
  3691. typecheckpass(left);
  3692. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3693. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3694. if codegenerror then
  3695. exit;
  3696. if target_specific_typecheck then
  3697. begin
  3698. // ok
  3699. end
  3700. else if (right.resultdef.typ=classrefdef) then
  3701. begin
  3702. { left maybe an interface reference }
  3703. if is_interfacecom(left.resultdef) or
  3704. is_javainterface(left.resultdef) then
  3705. begin
  3706. { relation checks are not possible }
  3707. end
  3708. { or left must be a class }
  3709. else if is_class(left.resultdef) or
  3710. is_javaclass(left.resultdef) then
  3711. begin
  3712. { the operands must be related }
  3713. if (not(def_is_related(tobjectdef(left.resultdef),
  3714. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3715. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  3716. tobjectdef(left.resultdef)))) then
  3717. CGMessage2(type_e_classes_not_related,
  3718. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3719. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3720. end
  3721. else
  3722. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3723. case nodetype of
  3724. isn:
  3725. resultdef:=pasbool8type;
  3726. asn:
  3727. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3728. end;
  3729. end
  3730. else if is_interface(right.resultdef) or
  3731. is_dispinterface(right.resultdef) or
  3732. is_javainterface(right.resultdef) then
  3733. begin
  3734. case nodetype of
  3735. isn:
  3736. resultdef:=pasbool8type;
  3737. asn:
  3738. resultdef:=right.resultdef;
  3739. end;
  3740. { left is a class or interface }
  3741. if is_javainterface(right.resultdef) then
  3742. begin
  3743. if not is_java_class_or_interface(left.resultdef) then
  3744. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3745. end
  3746. else if not(is_class(left.resultdef) or
  3747. is_interfacecom(left.resultdef)) then
  3748. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  3749. else
  3750. begin
  3751. { load the GUID of the interface }
  3752. if (right.nodetype=typen) then
  3753. begin
  3754. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3755. begin
  3756. if assigned(tobjectdef(right.resultdef).iidstr) then
  3757. begin
  3758. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3759. tstringconstnode(hp).changestringtype(cshortstringtype);
  3760. right.free;
  3761. right:=hp;
  3762. end
  3763. else
  3764. internalerror(201006131);
  3765. end
  3766. else
  3767. begin
  3768. if assigned(tobjectdef(right.resultdef).iidguid) then
  3769. begin
  3770. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3771. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3772. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3773. right.free;
  3774. right:=hp;
  3775. end
  3776. else
  3777. internalerror(201006132);
  3778. end;
  3779. typecheckpass(right);
  3780. end;
  3781. end;
  3782. end
  3783. else
  3784. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3785. end;
  3786. {*****************************************************************************
  3787. TISNODE
  3788. *****************************************************************************}
  3789. constructor tisnode.create(l,r : tnode);
  3790. begin
  3791. inherited create(isn,l,r);
  3792. end;
  3793. constructor tisnode.create_internal(l, r: tnode);
  3794. begin
  3795. create(l,r);
  3796. include(flags,nf_internal);
  3797. end;
  3798. function tisnode.pass_1 : tnode;
  3799. var
  3800. procname: string;
  3801. begin
  3802. result:=nil;
  3803. { Passing a class type to an "is" expression cannot result in a class
  3804. of that type to be constructed.
  3805. }
  3806. include(right.flags,nf_ignore_for_wpo);
  3807. if is_class(left.resultdef) and
  3808. (right.resultdef.typ=classrefdef) then
  3809. result := ccallnode.createinternres('fpc_do_is',
  3810. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3811. resultdef)
  3812. else
  3813. begin
  3814. if is_class(left.resultdef) then
  3815. if is_shortstring(right.resultdef) then
  3816. procname := 'fpc_class_is_corbaintf'
  3817. else
  3818. procname := 'fpc_class_is_intf'
  3819. else
  3820. if right.resultdef.typ=classrefdef then
  3821. procname := 'fpc_intf_is_class'
  3822. else
  3823. procname := 'fpc_intf_is';
  3824. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3825. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3826. end;
  3827. left := nil;
  3828. right := nil;
  3829. //firstpass(call);
  3830. if codegenerror then
  3831. exit;
  3832. end;
  3833. { dummy pass_2, it will never be called, but we need one since }
  3834. { you can't instantiate an abstract class }
  3835. procedure tisnode.pass_generate_code;
  3836. begin
  3837. end;
  3838. {*****************************************************************************
  3839. TASNODE
  3840. *****************************************************************************}
  3841. constructor tasnode.create(l,r : tnode);
  3842. begin
  3843. inherited create(asn,l,r);
  3844. call := nil;
  3845. end;
  3846. constructor tasnode.create_internal(l,r : tnode);
  3847. begin
  3848. create(l,r);
  3849. include(flags,nf_internal);
  3850. end;
  3851. destructor tasnode.destroy;
  3852. begin
  3853. call.free;
  3854. inherited destroy;
  3855. end;
  3856. function tasnode.dogetcopy: tnode;
  3857. begin
  3858. result := inherited dogetcopy;
  3859. if assigned(call) then
  3860. tasnode(result).call := call.getcopy
  3861. else
  3862. tasnode(result).call := nil;
  3863. end;
  3864. function tasnode.docompare(p: tnode): boolean;
  3865. begin
  3866. result:=
  3867. inherited docompare(p) and
  3868. tasnode(p).call.isequal(call);
  3869. end;
  3870. function tasnode.pass_1 : tnode;
  3871. var
  3872. procname: string;
  3873. begin
  3874. result:=nil;
  3875. { Passing a class type to an "as" expression cannot result in a class
  3876. of that type to be constructed.
  3877. We could put this inside the if-block below, but this way it is
  3878. safe for sure even if the code below changes
  3879. }
  3880. if assigned(right) then
  3881. include(right.flags,nf_ignore_for_wpo);
  3882. if not assigned(call) then
  3883. begin
  3884. if is_class(left.resultdef) and
  3885. (right.resultdef.typ=classrefdef) then
  3886. call := ccallnode.createinternres('fpc_do_as',
  3887. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3888. resultdef)
  3889. else
  3890. begin
  3891. if is_class(left.resultdef) then
  3892. if is_shortstring(right.resultdef) then
  3893. procname := 'fpc_class_as_corbaintf'
  3894. else
  3895. procname := 'fpc_class_as_intf'
  3896. else
  3897. if right.resultdef.typ=classrefdef then
  3898. procname := 'fpc_intf_as_class'
  3899. else
  3900. procname := 'fpc_intf_as';
  3901. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3902. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3903. end;
  3904. left := nil;
  3905. right := nil;
  3906. firstpass(call);
  3907. if codegenerror then
  3908. exit;
  3909. expectloc:=call.expectloc;
  3910. end;
  3911. end;
  3912. end.