ncnv.pas 158 KB

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