ncnv.pas 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169
  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 (resultdef.typ=stringdef) and
  2403. ((convtype=tc_equal) or
  2404. ((convtype=tc_string_2_string) and
  2405. (
  2406. ((not is_widechararray(left.resultdef) and
  2407. not is_wide_or_unicode_string(left.resultdef)) or
  2408. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  2409. )
  2410. )
  2411. )
  2412. ) then
  2413. begin
  2414. { output string consts in local ansistring encoding }
  2415. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0)or(tstringdef(resultdef).encoding=globals.CP_NONE)) then
  2416. tstringconstnode(left).changestringtype(getansistringdef)
  2417. else
  2418. tstringconstnode(left).changestringtype(resultdef);
  2419. result:=left;
  2420. resultdef:=left.resultdef;
  2421. left:=nil;
  2422. exit;
  2423. end;
  2424. realconstn :
  2425. begin
  2426. if (convtype = tc_real_2_currency) then
  2427. result := typecheck_real_to_currency
  2428. else if (convtype = tc_real_2_real) then
  2429. result := typecheck_real_to_real
  2430. else
  2431. exit;
  2432. if not(assigned(result)) then
  2433. begin
  2434. result := left;
  2435. left := nil;
  2436. end;
  2437. if (result.nodetype = realconstn) then
  2438. begin
  2439. hp:=result;
  2440. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2441. if ([nf_explicit,nf_internal] * flags <> []) then
  2442. include(result.flags, nf_explicit);
  2443. hp.free;
  2444. end;
  2445. end;
  2446. niln :
  2447. begin
  2448. { nil to ordinal node }
  2449. if (resultdef.typ=orddef) then
  2450. begin
  2451. hp:=cordconstnode.create(0,resultdef,true);
  2452. if ([nf_explicit,nf_internal] * flags <> []) then
  2453. include(hp.flags, nf_explicit);
  2454. result:=hp;
  2455. exit;
  2456. end
  2457. else
  2458. { fold nil to any pointer type }
  2459. if (resultdef.typ=pointerdef) then
  2460. begin
  2461. hp:=cnilnode.create;
  2462. hp.resultdef:=resultdef;
  2463. if ([nf_explicit,nf_internal] * flags <> []) then
  2464. include(hp.flags, nf_explicit);
  2465. result:=hp;
  2466. exit;
  2467. end
  2468. else
  2469. { remove typeconv after niln, but not when the result is a
  2470. methodpointer. The typeconv of the methodpointer will then
  2471. take care of updateing size of niln to OS_64 }
  2472. if not((resultdef.typ=procvardef) and
  2473. not(tprocvardef(resultdef).is_addressonly)) then
  2474. begin
  2475. left.resultdef:=resultdef;
  2476. if ([nf_explicit,nf_internal] * flags <> []) then
  2477. include(left.flags, nf_explicit);
  2478. result:=left;
  2479. left:=nil;
  2480. exit;
  2481. end;
  2482. end;
  2483. ordconstn :
  2484. begin
  2485. { ordinal contants can be directly converted }
  2486. { but not char to char because it is a widechar to char or via versa }
  2487. { which needs extra code to do the code page transistion }
  2488. { constant ordinal to pointer }
  2489. if (resultdef.typ=pointerdef) and
  2490. (convtype<>tc_cchar_2_pchar) then
  2491. begin
  2492. if (target_info.system in systems_managed_vm) and
  2493. (tordconstnode(left).value<>0) then
  2494. message(parser_e_feature_unsupported_for_vm);
  2495. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2496. if ([nf_explicit,nf_internal] * flags <> []) then
  2497. include(hp.flags, nf_explicit);
  2498. result:=hp;
  2499. exit;
  2500. end
  2501. else if is_ordinal(resultdef) and
  2502. not(convtype=tc_char_2_char) then
  2503. begin
  2504. { replace the resultdef and recheck the range }
  2505. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  2506. include(left.flags, nf_explicit)
  2507. else
  2508. { no longer an ordconst with an explicit typecast }
  2509. exclude(left.flags, nf_explicit);
  2510. { when converting from one boolean type to another, force }
  2511. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2512. { (Delphi-compatibile) }
  2513. if is_boolean(left.resultdef) and
  2514. is_boolean(resultdef) and
  2515. (is_cbool(left.resultdef) or
  2516. is_cbool(resultdef)) then
  2517. begin
  2518. if is_pasbool(resultdef) then
  2519. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2520. else
  2521. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2522. end
  2523. else
  2524. begin
  2525. { for constant values on absolute variables, swaping is required }
  2526. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2527. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  2528. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags)
  2529. or (nf_absolute in flags),false);
  2530. { swap value back, but according to new type }
  2531. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2532. swap_const_value(tordconstnode(left).value,resultdef.size);
  2533. end;
  2534. left.resultdef:=resultdef;
  2535. tordconstnode(left).typedef:=resultdef;
  2536. if is_signed(resultdef) then
  2537. tordconstnode(left).value.signed:=true
  2538. else
  2539. tordconstnode(left).value.signed:=false;
  2540. result:=left;
  2541. left:=nil;
  2542. exit;
  2543. end;
  2544. end;
  2545. pointerconstn :
  2546. begin
  2547. { pointerconstn to any pointer is folded too }
  2548. if (resultdef.typ=pointerdef) then
  2549. begin
  2550. left.resultdef:=resultdef;
  2551. if ([nf_explicit,nf_internal] * flags <> []) then
  2552. include(left.flags, nf_explicit)
  2553. else
  2554. { no longer an ordconst with an explicit typecast }
  2555. exclude(left.flags, nf_explicit);
  2556. result:=left;
  2557. left:=nil;
  2558. exit;
  2559. end
  2560. { constant pointer to ordinal }
  2561. else if is_ordinal(resultdef) then
  2562. begin
  2563. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2564. resultdef,not(nf_explicit in flags));
  2565. if ([nf_explicit,nf_internal] * flags <> []) then
  2566. include(hp.flags, nf_explicit);
  2567. result:=hp;
  2568. exit;
  2569. end;
  2570. end;
  2571. end;
  2572. {$ifndef CPUNO32BITOPS}
  2573. { must be done before code below, because we need the
  2574. typeconversions for ordconstn's as well }
  2575. case convtype of
  2576. tc_int_2_int:
  2577. begin
  2578. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2579. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  2580. begin
  2581. { avoid unnecessary widening of intermediary calculations
  2582. to 64 bit }
  2583. if (resultdef.size <= 4) and
  2584. is_64bitint(left.resultdef) and
  2585. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2586. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  2587. doremoveinttypeconvs(left,generrordef,not foundsint,s32inttype,u32inttype);
  2588. {$if defined(cpu8bitalu)}
  2589. if (resultdef.size<left.resultdef.size) and
  2590. is_integer(left.resultdef) and
  2591. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2592. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  2593. doremoveinttypeconvs(left,generrordef,not foundsint,s8inttype,u8inttype);
  2594. {$endif defined(cpu8bitalu)}
  2595. end;
  2596. end;
  2597. end;
  2598. {$endif not CPUNO32BITOPS}
  2599. end;
  2600. procedure Ttypeconvnode.mark_write;
  2601. begin
  2602. left.mark_write;
  2603. end;
  2604. function ttypeconvnode.first_cord_to_pointer : tnode;
  2605. begin
  2606. result:=nil;
  2607. internalerror(200104043);
  2608. end;
  2609. function ttypeconvnode.first_int_to_int : tnode;
  2610. begin
  2611. first_int_to_int:=nil;
  2612. expectloc:=left.expectloc;
  2613. if not is_void(left.resultdef) then
  2614. begin
  2615. if (left.expectloc<>LOC_REGISTER) and
  2616. ((resultdef.size>left.resultdef.size) or
  2617. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2618. expectloc:=LOC_REGISTER
  2619. else
  2620. if (left.expectloc=LOC_CREGISTER) and
  2621. (resultdef.size<left.resultdef.size) then
  2622. expectloc:=LOC_REGISTER;
  2623. end;
  2624. end;
  2625. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2626. begin
  2627. result:=nil;
  2628. expectloc:=LOC_REGISTER;
  2629. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2630. if (cs_create_pic in current_settings.moduleswitches) then
  2631. include(current_procinfo.flags,pi_needs_got);
  2632. end;
  2633. function ttypeconvnode.first_cstring_to_int : tnode;
  2634. begin
  2635. result:=nil;
  2636. internalerror(200510014);
  2637. end;
  2638. function ttypeconvnode.first_string_to_chararray : tnode;
  2639. begin
  2640. first_string_to_chararray:=nil;
  2641. expectloc:=left.expectloc;
  2642. end;
  2643. function ttypeconvnode.first_char_to_string : tnode;
  2644. begin
  2645. first_char_to_string:=nil;
  2646. if tstringdef(resultdef).stringtype=st_shortstring then
  2647. inc(current_procinfo.estimatedtempsize,256);
  2648. expectloc:=LOC_REFERENCE;
  2649. end;
  2650. function ttypeconvnode.first_char_to_chararray : tnode;
  2651. begin
  2652. if resultdef.size <> 1 then
  2653. begin
  2654. { convert first to string, then to chararray }
  2655. inserttypeconv(left,cshortstringtype);
  2656. inserttypeconv(left,resultdef);
  2657. result:=left;
  2658. left := nil;
  2659. exit;
  2660. end;
  2661. result := nil;
  2662. end;
  2663. function ttypeconvnode.first_nothing : tnode;
  2664. begin
  2665. first_nothing:=nil;
  2666. end;
  2667. function ttypeconvnode.first_array_to_pointer : tnode;
  2668. begin
  2669. first_array_to_pointer:=nil;
  2670. expectloc:=LOC_REGISTER;
  2671. end;
  2672. function ttypeconvnode.first_int_to_real: tnode;
  2673. var
  2674. fname: string[32];
  2675. begin
  2676. if target_info.system in systems_wince then
  2677. begin
  2678. { converting a 64bit integer to a float requires a helper }
  2679. if is_64bitint(left.resultdef) or
  2680. is_currency(left.resultdef) then
  2681. begin
  2682. { hack to avoid double division by 10000, as it's
  2683. already done by typecheckpass.resultdef_int_to_real }
  2684. if is_currency(left.resultdef) then
  2685. left.resultdef := s64inttype;
  2686. if is_signed(left.resultdef) then
  2687. fname:='i64to'
  2688. else
  2689. fname:='ui64to';
  2690. end
  2691. else
  2692. { other integers are supposed to be 32 bit }
  2693. begin
  2694. if is_signed(left.resultdef) then
  2695. fname:='ito'
  2696. else
  2697. fname:='uto';
  2698. firstpass(left);
  2699. end;
  2700. if tfloatdef(resultdef).floattype=s64real then
  2701. fname:=fname+'d'
  2702. else
  2703. fname:=fname+'s';
  2704. result:=ccallnode.createintern(fname,ccallparanode.create(
  2705. left,nil));
  2706. left:=nil;
  2707. firstpass(result);
  2708. exit;
  2709. end
  2710. else
  2711. begin
  2712. { converting a 64bit integer to a float requires a helper }
  2713. if is_64bitint(left.resultdef) or
  2714. is_currency(left.resultdef) then
  2715. begin
  2716. { hack to avoid double division by 10000, as it's
  2717. already done by typecheckpass.resultdef_int_to_real }
  2718. if is_currency(left.resultdef) then
  2719. left.resultdef := s64inttype;
  2720. if is_signed(left.resultdef) then
  2721. fname:='int64_to_'
  2722. else
  2723. { we can't do better currently }
  2724. fname:='qword_to_';
  2725. end
  2726. else
  2727. { other integers are supposed to be 32 bit }
  2728. begin
  2729. if is_signed(left.resultdef) then
  2730. fname:='int32_to_'
  2731. else
  2732. fname:='int64_to_';
  2733. firstpass(left);
  2734. end;
  2735. if tfloatdef(resultdef).floattype=s64real then
  2736. fname:=fname+'float64'
  2737. else
  2738. fname:=fname+'float32';
  2739. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2740. left,nil)),resultdef);
  2741. left:=nil;
  2742. firstpass(result);
  2743. exit;
  2744. end;
  2745. end;
  2746. function ttypeconvnode.first_real_to_real : tnode;
  2747. begin
  2748. {$ifdef cpufpemu}
  2749. if cs_fp_emulation in current_settings.moduleswitches then
  2750. begin
  2751. if target_info.system in systems_wince then
  2752. begin
  2753. case tfloatdef(left.resultdef).floattype of
  2754. s32real:
  2755. case tfloatdef(resultdef).floattype of
  2756. s64real:
  2757. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  2758. s32real:
  2759. begin
  2760. result:=left;
  2761. left:=nil;
  2762. end;
  2763. else
  2764. internalerror(2005082704);
  2765. end;
  2766. s64real:
  2767. case tfloatdef(resultdef).floattype of
  2768. s32real:
  2769. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  2770. s64real:
  2771. begin
  2772. result:=left;
  2773. left:=nil;
  2774. end;
  2775. else
  2776. internalerror(2005082703);
  2777. end;
  2778. else
  2779. internalerror(2005082702);
  2780. end;
  2781. left:=nil;
  2782. firstpass(result);
  2783. exit;
  2784. end
  2785. else
  2786. begin
  2787. case tfloatdef(left.resultdef).floattype of
  2788. s32real:
  2789. case tfloatdef(resultdef).floattype of
  2790. s64real:
  2791. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2792. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2793. s32real:
  2794. begin
  2795. result:=left;
  2796. left:=nil;
  2797. end;
  2798. else
  2799. internalerror(200610151);
  2800. end;
  2801. s64real:
  2802. case tfloatdef(resultdef).floattype of
  2803. s32real:
  2804. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2805. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2806. s64real:
  2807. begin
  2808. result:=left;
  2809. left:=nil;
  2810. end;
  2811. else
  2812. internalerror(200610152);
  2813. end;
  2814. else
  2815. internalerror(200610153);
  2816. end;
  2817. left:=nil;
  2818. firstpass(result);
  2819. exit;
  2820. end;
  2821. end
  2822. else
  2823. {$endif cpufpemu}
  2824. begin
  2825. first_real_to_real:=nil;
  2826. if not use_vectorfpu(resultdef) then
  2827. expectloc:=LOC_FPUREGISTER
  2828. else
  2829. expectloc:=LOC_MMREGISTER;
  2830. end;
  2831. end;
  2832. function ttypeconvnode.first_pointer_to_array : tnode;
  2833. begin
  2834. first_pointer_to_array:=nil;
  2835. expectloc:=LOC_REFERENCE;
  2836. end;
  2837. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2838. begin
  2839. first_cchar_to_pchar:=nil;
  2840. internalerror(200104021);
  2841. end;
  2842. function ttypeconvnode.first_bool_to_int : tnode;
  2843. begin
  2844. first_bool_to_int:=nil;
  2845. { byte(boolean) or word(wordbool) or longint(longbool) must
  2846. be accepted for var parameters }
  2847. if (nf_explicit in flags) and
  2848. (left.resultdef.size=resultdef.size) and
  2849. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2850. begin
  2851. expectloc:=left.expectloc;
  2852. exit;
  2853. end;
  2854. expectloc:=LOC_REGISTER;
  2855. end;
  2856. function ttypeconvnode.first_int_to_bool : tnode;
  2857. begin
  2858. first_int_to_bool:=nil;
  2859. { byte(boolean) or word(wordbool) or longint(longbool) must
  2860. be accepted for var parameters }
  2861. if (nf_explicit in flags) and
  2862. (left.resultdef.size=resultdef.size) and
  2863. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2864. begin
  2865. expectloc:=left.expectloc;
  2866. exit;
  2867. end;
  2868. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2869. { convert to a boolean (only necessary for 32bit processors) }
  2870. { note: not if left is already a bool (qwordbool that is true, even if
  2871. only because the highest bit is set, must remain true if it is
  2872. --implicitly, unlike integers-- converted to another type of bool),
  2873. Left can already be a bool because this routine can also be called
  2874. from first_bool_to_bool }
  2875. if not is_boolean(left.resultdef) and
  2876. (left.resultdef.size > sizeof(aint)) and
  2877. (left.resultdef.size<>resultdef.size)
  2878. and is_cbool(resultdef) then
  2879. begin
  2880. left:=ctypeconvnode.create_internal(left,s32inttype);
  2881. firstpass(left);
  2882. exit;
  2883. end;
  2884. expectloc:=LOC_REGISTER;
  2885. end;
  2886. function ttypeconvnode.first_bool_to_bool : tnode;
  2887. begin
  2888. first_bool_to_bool:=nil;
  2889. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  2890. not is_cbool(resultdef) then
  2891. expectloc := left.expectloc
  2892. { the following cases use the code generation for bool_to_int/
  2893. int_to_bool -> also set their expectlocs }
  2894. else if (resultdef.size=left.resultdef.size) and
  2895. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  2896. result:=first_bool_to_int
  2897. else
  2898. result:=first_int_to_bool
  2899. end;
  2900. function ttypeconvnode.first_char_to_char : tnode;
  2901. var
  2902. fname: string[18];
  2903. begin
  2904. if (torddef(resultdef).ordtype=uchar) and
  2905. (torddef(left.resultdef).ordtype=uwidechar) then
  2906. fname := 'fpc_uchar_to_char'
  2907. else if (torddef(resultdef).ordtype=uwidechar) and
  2908. (torddef(left.resultdef).ordtype=uchar) then
  2909. fname := 'fpc_char_to_uchar'
  2910. else
  2911. internalerror(2007081201);
  2912. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2913. left:=nil;
  2914. firstpass(result);
  2915. end;
  2916. function ttypeconvnode.first_proc_to_procvar : tnode;
  2917. begin
  2918. first_proc_to_procvar:=nil;
  2919. { if we take the address of a nested function, the current function/
  2920. procedure needs a stack frame since it's required to construct
  2921. the nested procvar }
  2922. if is_nested_pd(tprocvardef(resultdef)) then
  2923. include(current_procinfo.flags,pi_needs_stackframe);
  2924. if tabstractprocdef(resultdef).is_addressonly then
  2925. expectloc:=LOC_REGISTER
  2926. else
  2927. expectloc:=left.expectloc;
  2928. end;
  2929. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2930. begin
  2931. first_nil_to_methodprocvar:=nil;
  2932. expectloc:=LOC_REGISTER;
  2933. end;
  2934. function ttypeconvnode.first_set_to_set : tnode;
  2935. var
  2936. newstatement : tstatementnode;
  2937. temp : ttempcreatenode;
  2938. begin
  2939. { in theory, we should do range checking here,
  2940. but Delphi doesn't do it either (FK) }
  2941. if left.nodetype=setconstn then
  2942. begin
  2943. left.resultdef:=resultdef;
  2944. result:=left;
  2945. left:=nil;
  2946. end
  2947. { equal sets for the code generator? }
  2948. else if (left.resultdef.size=resultdef.size) and
  2949. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2950. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2951. { e.g., this prints true because of this:
  2952. var
  2953. sa: set of 1..2;
  2954. sb: set of 5..6;
  2955. b: byte;
  2956. begin
  2957. b:=1;
  2958. sa:=[1..2];
  2959. sb:=sa;
  2960. writeln(b in sb);
  2961. end.
  2962. }
  2963. begin
  2964. result:=left;
  2965. left:=nil;
  2966. end
  2967. else
  2968. begin
  2969. result:=internalstatements(newstatement);
  2970. { in case left is a smallset expression, it can be an addn or so. }
  2971. { fpc_varset_load expects a formal const parameter, which doesn't }
  2972. { accept set addn's -> assign to a temp first and pass the temp }
  2973. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2974. begin
  2975. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2976. addstatement(newstatement,temp);
  2977. { temp := left }
  2978. addstatement(newstatement,cassignmentnode.create(
  2979. ctemprefnode.create(temp),left));
  2980. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2981. addstatement(newstatement,ctemprefnode.create(temp));
  2982. left:=result;
  2983. firstpass(left);
  2984. { recreate the result's internalstatements list }
  2985. result:=internalstatements(newstatement);
  2986. end;
  2987. { create temp for result }
  2988. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2989. addstatement(newstatement,temp);
  2990. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2991. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2992. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2993. ccallparanode.create(ctemprefnode.create(temp),
  2994. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2995. ccallparanode.create(left,nil))))))
  2996. );
  2997. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2998. addstatement(newstatement,ctemprefnode.create(temp));
  2999. left:=nil;
  3000. end;
  3001. end;
  3002. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  3003. begin
  3004. first_ansistring_to_pchar:=nil;
  3005. expectloc:=LOC_REGISTER;
  3006. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  3007. if (cs_create_pic in current_settings.moduleswitches) then
  3008. include(current_procinfo.flags,pi_needs_got);
  3009. end;
  3010. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  3011. begin
  3012. first_arrayconstructor_to_set:=nil;
  3013. internalerror(200104022);
  3014. end;
  3015. function ttypeconvnode.first_class_to_intf : tnode;
  3016. var
  3017. hd : tobjectdef;
  3018. ImplIntf : TImplementedInterface;
  3019. begin
  3020. result:=nil;
  3021. expectloc:=LOC_REGISTER;
  3022. hd:=tobjectdef(left.resultdef);
  3023. while assigned(hd) do
  3024. begin
  3025. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3026. if assigned(ImplIntf) then
  3027. begin
  3028. case ImplIntf.IType of
  3029. etStandard:
  3030. { handle in pass 2 }
  3031. ;
  3032. etFieldValue, etFieldValueClass:
  3033. if is_interface(tobjectdef(resultdef)) then
  3034. begin
  3035. result:=left;
  3036. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3037. { this ensures proper refcounting when field is of class type }
  3038. if not is_interface(result.resultdef) then
  3039. inserttypeconv(result, resultdef);
  3040. left:=nil;
  3041. end
  3042. else
  3043. begin
  3044. internalerror(200802213);
  3045. end;
  3046. etStaticMethodResult, etStaticMethodClass,
  3047. etVirtualMethodResult, etVirtualMethodClass:
  3048. if is_interface(tobjectdef(resultdef)) then
  3049. begin
  3050. { TODO: generating a call to TObject.GetInterface instead could yield
  3051. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3052. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3053. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3054. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3055. left,[],nil);
  3056. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3057. { if it is a class, process it further in a similar way }
  3058. if not is_interface(result.resultdef) then
  3059. inserttypeconv(result, resultdef);
  3060. left:=nil;
  3061. end
  3062. else if is_class(tobjectdef(resultdef)) then
  3063. begin
  3064. internalerror(200802211);
  3065. end
  3066. else
  3067. internalerror(200802231);
  3068. else
  3069. internalerror(200802165);
  3070. end;
  3071. break;
  3072. end;
  3073. hd:=hd.childof;
  3074. end;
  3075. if hd=nil then
  3076. internalerror(200802164);
  3077. end;
  3078. function ttypeconvnode.first_string_to_string : tnode;
  3079. var
  3080. procname: string[31];
  3081. newblock : tblocknode;
  3082. newstat : tstatementnode;
  3083. restemp : ttempcreatenode;
  3084. begin
  3085. { get the correct procedure name }
  3086. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3087. '_to_'+tstringdef(resultdef).stringtypname;
  3088. if tstringdef(resultdef).stringtype=st_shortstring then
  3089. begin
  3090. newblock:=internalstatements(newstat);
  3091. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3092. addstatement(newstat,restemp);
  3093. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3094. ctemprefnode.create(restemp),nil))));
  3095. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  3096. addstatement(newstat,ctemprefnode.create(restemp));
  3097. result:=newblock;
  3098. end
  3099. { encoding parameter required? }
  3100. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  3101. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  3102. result:=ccallnode.createinternres(procname,
  3103. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  3104. ccallparanode.create(left,nil)),resultdef)
  3105. else
  3106. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  3107. left:=nil;
  3108. end;
  3109. function ttypeconvnode._first_int_to_int : tnode;
  3110. begin
  3111. result:=first_int_to_int;
  3112. end;
  3113. function ttypeconvnode._first_cstring_to_pchar : tnode;
  3114. begin
  3115. result:=first_cstring_to_pchar;
  3116. end;
  3117. function ttypeconvnode._first_cstring_to_int : tnode;
  3118. begin
  3119. result:=first_cstring_to_int;
  3120. end;
  3121. function ttypeconvnode._first_string_to_chararray : tnode;
  3122. begin
  3123. result:=first_string_to_chararray;
  3124. end;
  3125. function ttypeconvnode._first_char_to_string : tnode;
  3126. begin
  3127. result:=first_char_to_string;
  3128. end;
  3129. function ttypeconvnode._first_char_to_chararray: tnode;
  3130. begin
  3131. result:=first_char_to_chararray;
  3132. end;
  3133. function ttypeconvnode._first_nothing : tnode;
  3134. begin
  3135. result:=first_nothing;
  3136. end;
  3137. function ttypeconvnode._first_array_to_pointer : tnode;
  3138. begin
  3139. result:=first_array_to_pointer;
  3140. end;
  3141. function ttypeconvnode._first_int_to_real : tnode;
  3142. begin
  3143. result:=first_int_to_real;
  3144. end;
  3145. function ttypeconvnode._first_real_to_real : tnode;
  3146. begin
  3147. result:=first_real_to_real;
  3148. end;
  3149. function ttypeconvnode._first_pointer_to_array : tnode;
  3150. begin
  3151. result:=first_pointer_to_array;
  3152. end;
  3153. function ttypeconvnode._first_cchar_to_pchar : tnode;
  3154. begin
  3155. result:=first_cchar_to_pchar;
  3156. end;
  3157. function ttypeconvnode._first_bool_to_int : tnode;
  3158. begin
  3159. result:=first_bool_to_int;
  3160. end;
  3161. function ttypeconvnode._first_int_to_bool : tnode;
  3162. begin
  3163. result:=first_int_to_bool;
  3164. end;
  3165. function ttypeconvnode._first_bool_to_bool : tnode;
  3166. begin
  3167. result:=first_bool_to_bool;
  3168. end;
  3169. function ttypeconvnode._first_proc_to_procvar : tnode;
  3170. begin
  3171. result:=first_proc_to_procvar;
  3172. end;
  3173. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  3174. begin
  3175. result:=first_nil_to_methodprocvar;
  3176. end;
  3177. function ttypeconvnode._first_set_to_set : tnode;
  3178. begin
  3179. result:=first_set_to_set;
  3180. end;
  3181. function ttypeconvnode._first_cord_to_pointer : tnode;
  3182. begin
  3183. result:=first_cord_to_pointer;
  3184. end;
  3185. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  3186. begin
  3187. result:=first_ansistring_to_pchar;
  3188. end;
  3189. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  3190. begin
  3191. result:=first_arrayconstructor_to_set;
  3192. end;
  3193. function ttypeconvnode._first_class_to_intf : tnode;
  3194. begin
  3195. result:=first_class_to_intf;
  3196. end;
  3197. function ttypeconvnode._first_char_to_char : tnode;
  3198. begin
  3199. result:=first_char_to_char;
  3200. end;
  3201. function ttypeconvnode._first_string_to_string : tnode;
  3202. begin
  3203. result:=first_string_to_string;
  3204. end;
  3205. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  3206. const
  3207. firstconvert : array[tconverttype] of pointer = (
  3208. nil, { none }
  3209. @ttypeconvnode._first_nothing, {equal}
  3210. @ttypeconvnode._first_nothing, {not_possible}
  3211. @ttypeconvnode._first_string_to_string,
  3212. @ttypeconvnode._first_char_to_string,
  3213. @ttypeconvnode._first_char_to_chararray,
  3214. nil, { removed in typecheck_chararray_to_string }
  3215. @ttypeconvnode._first_cchar_to_pchar,
  3216. @ttypeconvnode._first_cstring_to_pchar,
  3217. @ttypeconvnode._first_cstring_to_int,
  3218. @ttypeconvnode._first_ansistring_to_pchar,
  3219. @ttypeconvnode._first_string_to_chararray,
  3220. nil, { removed in typecheck_chararray_to_string }
  3221. @ttypeconvnode._first_array_to_pointer,
  3222. @ttypeconvnode._first_pointer_to_array,
  3223. @ttypeconvnode._first_int_to_int,
  3224. @ttypeconvnode._first_int_to_bool,
  3225. @ttypeconvnode._first_bool_to_bool,
  3226. @ttypeconvnode._first_bool_to_int,
  3227. @ttypeconvnode._first_real_to_real,
  3228. @ttypeconvnode._first_int_to_real,
  3229. nil, { removed in typecheck_real_to_currency }
  3230. @ttypeconvnode._first_proc_to_procvar,
  3231. @ttypeconvnode._first_nil_to_methodprocvar,
  3232. @ttypeconvnode._first_arrayconstructor_to_set,
  3233. @ttypeconvnode._first_set_to_set,
  3234. @ttypeconvnode._first_cord_to_pointer,
  3235. @ttypeconvnode._first_nothing,
  3236. @ttypeconvnode._first_nothing,
  3237. @ttypeconvnode._first_class_to_intf,
  3238. @ttypeconvnode._first_char_to_char,
  3239. @ttypeconvnode._first_nothing,
  3240. @ttypeconvnode._first_nothing,
  3241. nil,
  3242. nil,
  3243. nil,
  3244. nil,
  3245. nil,
  3246. nil,
  3247. nil,
  3248. @ttypeconvnode._first_nothing
  3249. );
  3250. type
  3251. tprocedureofobject = function : tnode of object;
  3252. var
  3253. r : packed record
  3254. proc : pointer;
  3255. obj : pointer;
  3256. end;
  3257. begin
  3258. { this is a little bit dirty but it works }
  3259. { and should be quite portable too }
  3260. r.proc:=firstconvert[c];
  3261. r.obj:=self;
  3262. if not assigned(r.proc) then
  3263. internalerror(200312081);
  3264. first_call_helper:=tprocedureofobject(r)()
  3265. end;
  3266. function ttypeconvnode.pass_1 : tnode;
  3267. begin
  3268. if warn_pointer_to_signed then
  3269. cgmessage(type_w_pointer_to_signed);
  3270. result:=nil;
  3271. firstpass(left);
  3272. if codegenerror then
  3273. exit;
  3274. expectloc:=left.expectloc;
  3275. if nf_explicit in flags then
  3276. { check if the result could be in a register }
  3277. if (not(tstoreddef(resultdef).is_intregable) and
  3278. not(tstoreddef(resultdef).is_fpuregable)) or
  3279. ((left.resultdef.typ = floatdef) and
  3280. (resultdef.typ <> floatdef)) then
  3281. make_not_regable(left,[ra_addr_regable]);
  3282. result:=first_call_helper(convtype);
  3283. end;
  3284. function ttypeconvnode.retains_value_location:boolean;
  3285. begin
  3286. result:=(convtype=tc_equal) or
  3287. { typecasting from void is always allowed }
  3288. is_void(left.resultdef) or
  3289. (left.resultdef.typ=formaldef) or
  3290. { int 2 int with same size reuses same location, or for
  3291. tp7 mode also allow size < orignal size }
  3292. (
  3293. (convtype=tc_int_2_int) and
  3294. (
  3295. not is_bitpacked_access(left) and
  3296. (resultdef.size=left.resultdef.size) or
  3297. ((m_tp7 in current_settings.modeswitches) and
  3298. (resultdef.size<left.resultdef.size))
  3299. )
  3300. ) or
  3301. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  3302. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  3303. (nf_explicit in flags) and
  3304. (resultdef.size=left.resultdef.size)) or
  3305. { on managed platforms, converting an element to an open array
  3306. involves creating an actual array -> value location changes }
  3307. ((convtype=tc_elem_2_openarray) and
  3308. not(target_info.system in systems_managed_vm));
  3309. end;
  3310. function ttypeconvnode.assign_allowed:boolean;
  3311. begin
  3312. result:=retains_value_location;
  3313. { When using only a part of the value it can't be in a register since
  3314. that will load the value in a new register first }
  3315. { the same goes for changing the sign of equal-sized values which
  3316. are smaller than an entire register }
  3317. if result and
  3318. { don't try to check the size of an open array }
  3319. (is_open_array(resultdef) or
  3320. (resultdef.size<left.resultdef.size) or
  3321. ((resultdef.size=left.resultdef.size) and
  3322. (left.resultdef.size<sizeof(aint)) and
  3323. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  3324. make_not_regable(left,[ra_addr_regable]);
  3325. end;
  3326. function ttypeconvnode.docompare(p: tnode) : boolean;
  3327. begin
  3328. docompare :=
  3329. inherited docompare(p) and
  3330. (convtype = ttypeconvnode(p).convtype) and
  3331. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  3332. end;
  3333. procedure ttypeconvnode._second_int_to_int;
  3334. begin
  3335. second_int_to_int;
  3336. end;
  3337. procedure ttypeconvnode._second_string_to_string;
  3338. begin
  3339. second_string_to_string;
  3340. end;
  3341. procedure ttypeconvnode._second_cstring_to_pchar;
  3342. begin
  3343. second_cstring_to_pchar;
  3344. end;
  3345. procedure ttypeconvnode._second_cstring_to_int;
  3346. begin
  3347. second_cstring_to_int;
  3348. end;
  3349. procedure ttypeconvnode._second_string_to_chararray;
  3350. begin
  3351. second_string_to_chararray;
  3352. end;
  3353. procedure ttypeconvnode._second_array_to_pointer;
  3354. begin
  3355. second_array_to_pointer;
  3356. end;
  3357. procedure ttypeconvnode._second_pointer_to_array;
  3358. begin
  3359. second_pointer_to_array;
  3360. end;
  3361. procedure ttypeconvnode._second_chararray_to_string;
  3362. begin
  3363. second_chararray_to_string;
  3364. end;
  3365. procedure ttypeconvnode._second_char_to_string;
  3366. begin
  3367. second_char_to_string;
  3368. end;
  3369. procedure ttypeconvnode._second_int_to_real;
  3370. begin
  3371. second_int_to_real;
  3372. end;
  3373. procedure ttypeconvnode._second_real_to_real;
  3374. begin
  3375. second_real_to_real;
  3376. end;
  3377. procedure ttypeconvnode._second_cord_to_pointer;
  3378. begin
  3379. second_cord_to_pointer;
  3380. end;
  3381. procedure ttypeconvnode._second_proc_to_procvar;
  3382. begin
  3383. second_proc_to_procvar;
  3384. end;
  3385. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3386. begin
  3387. second_nil_to_methodprocvar;
  3388. end;
  3389. procedure ttypeconvnode._second_bool_to_int;
  3390. begin
  3391. second_bool_to_int;
  3392. end;
  3393. procedure ttypeconvnode._second_int_to_bool;
  3394. begin
  3395. second_int_to_bool;
  3396. end;
  3397. procedure ttypeconvnode._second_bool_to_bool;
  3398. begin
  3399. second_bool_to_bool;
  3400. end;
  3401. procedure ttypeconvnode._second_set_to_set;
  3402. begin
  3403. second_set_to_set;
  3404. end;
  3405. procedure ttypeconvnode._second_ansistring_to_pchar;
  3406. begin
  3407. second_ansistring_to_pchar;
  3408. end;
  3409. procedure ttypeconvnode._second_class_to_intf;
  3410. begin
  3411. second_class_to_intf;
  3412. end;
  3413. procedure ttypeconvnode._second_char_to_char;
  3414. begin
  3415. second_char_to_char;
  3416. end;
  3417. procedure ttypeconvnode._second_elem_to_openarray;
  3418. begin
  3419. second_elem_to_openarray;
  3420. end;
  3421. procedure ttypeconvnode._second_nothing;
  3422. begin
  3423. second_nothing;
  3424. end;
  3425. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3426. const
  3427. secondconvert : array[tconverttype] of pointer = (
  3428. @ttypeconvnode._second_nothing, {none}
  3429. @ttypeconvnode._second_nothing, {equal}
  3430. @ttypeconvnode._second_nothing, {not_possible}
  3431. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3432. @ttypeconvnode._second_char_to_string,
  3433. @ttypeconvnode._second_nothing, {char_to_charray}
  3434. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3435. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3436. @ttypeconvnode._second_cstring_to_pchar,
  3437. @ttypeconvnode._second_cstring_to_int,
  3438. @ttypeconvnode._second_ansistring_to_pchar,
  3439. @ttypeconvnode._second_string_to_chararray,
  3440. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3441. @ttypeconvnode._second_array_to_pointer,
  3442. @ttypeconvnode._second_pointer_to_array,
  3443. @ttypeconvnode._second_int_to_int,
  3444. @ttypeconvnode._second_int_to_bool,
  3445. @ttypeconvnode._second_bool_to_bool,
  3446. @ttypeconvnode._second_bool_to_int,
  3447. @ttypeconvnode._second_real_to_real,
  3448. @ttypeconvnode._second_int_to_real,
  3449. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3450. @ttypeconvnode._second_proc_to_procvar,
  3451. @ttypeconvnode._second_nil_to_methodprocvar,
  3452. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3453. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3454. @ttypeconvnode._second_cord_to_pointer,
  3455. @ttypeconvnode._second_nothing, { interface 2 string }
  3456. @ttypeconvnode._second_nothing, { interface 2 guid }
  3457. @ttypeconvnode._second_class_to_intf,
  3458. @ttypeconvnode._second_char_to_char,
  3459. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3460. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3461. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3462. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3463. @ttypeconvnode._second_nothing, { variant_2_enum }
  3464. @ttypeconvnode._second_nothing, { enum_2_variant }
  3465. @ttypeconvnode._second_nothing, { variant_2_interface }
  3466. @ttypeconvnode._second_nothing, { interface_2_variant }
  3467. @ttypeconvnode._second_nothing, { array_2_dynarray }
  3468. @ttypeconvnode._second_elem_to_openarray { elem_2_openarray }
  3469. );
  3470. type
  3471. tprocedureofobject = procedure of object;
  3472. var
  3473. r : packed record
  3474. proc : pointer;
  3475. obj : pointer;
  3476. end;
  3477. begin
  3478. { this is a little bit dirty but it works }
  3479. { and should be quite portable too }
  3480. r.proc:=secondconvert[c];
  3481. r.obj:=self;
  3482. tprocedureofobject(r)();
  3483. end;
  3484. {*****************************************************************************
  3485. TASNODE
  3486. *****************************************************************************}
  3487. function tasisnode.target_specific_typecheck: boolean;
  3488. begin
  3489. result:=false;
  3490. end;
  3491. function tasisnode.pass_typecheck: tnode;
  3492. var
  3493. hp : tnode;
  3494. begin
  3495. result:=nil;
  3496. typecheckpass(right);
  3497. typecheckpass(left);
  3498. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3499. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3500. if codegenerror then
  3501. exit;
  3502. if target_specific_typecheck then
  3503. begin
  3504. // ok
  3505. end
  3506. else if (right.resultdef.typ=classrefdef) then
  3507. begin
  3508. { left maybe an interface reference }
  3509. if is_interfacecom(left.resultdef) or
  3510. is_javainterface(left.resultdef) then
  3511. begin
  3512. { relation checks are not possible }
  3513. end
  3514. { or left must be a class }
  3515. else if is_class(left.resultdef) or
  3516. is_javaclass(left.resultdef) then
  3517. begin
  3518. { the operands must be related }
  3519. if (not(def_is_related(tobjectdef(left.resultdef),
  3520. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3521. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  3522. tobjectdef(left.resultdef)))) then
  3523. CGMessage2(type_e_classes_not_related,
  3524. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3525. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3526. end
  3527. else
  3528. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3529. case nodetype of
  3530. isn:
  3531. resultdef:=pasbool8type;
  3532. asn:
  3533. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3534. end;
  3535. end
  3536. else if is_interface(right.resultdef) or
  3537. is_dispinterface(right.resultdef) or
  3538. is_javainterface(right.resultdef) then
  3539. begin
  3540. case nodetype of
  3541. isn:
  3542. resultdef:=pasbool8type;
  3543. asn:
  3544. resultdef:=right.resultdef;
  3545. end;
  3546. { left is a class or interface }
  3547. if is_javainterface(right.resultdef) then
  3548. begin
  3549. if not is_java_class_or_interface(left.resultdef) then
  3550. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3551. end
  3552. else if not(is_class(left.resultdef) or
  3553. is_interfacecom(left.resultdef)) then
  3554. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  3555. else
  3556. begin
  3557. { load the GUID of the interface }
  3558. if (right.nodetype=typen) then
  3559. begin
  3560. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3561. begin
  3562. if assigned(tobjectdef(right.resultdef).iidstr) then
  3563. begin
  3564. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3565. tstringconstnode(hp).changestringtype(cshortstringtype);
  3566. right.free;
  3567. right:=hp;
  3568. end
  3569. else
  3570. internalerror(201006131);
  3571. end
  3572. else
  3573. begin
  3574. if assigned(tobjectdef(right.resultdef).iidguid) then
  3575. begin
  3576. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3577. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3578. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3579. right.free;
  3580. right:=hp;
  3581. end
  3582. else
  3583. internalerror(201006132);
  3584. end;
  3585. typecheckpass(right);
  3586. end;
  3587. end;
  3588. end
  3589. else
  3590. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3591. end;
  3592. {*****************************************************************************
  3593. TISNODE
  3594. *****************************************************************************}
  3595. constructor tisnode.create(l,r : tnode);
  3596. begin
  3597. inherited create(isn,l,r);
  3598. end;
  3599. constructor tisnode.create_internal(l, r: tnode);
  3600. begin
  3601. create(l,r);
  3602. include(flags,nf_internal);
  3603. end;
  3604. function tisnode.pass_1 : tnode;
  3605. var
  3606. procname: string;
  3607. begin
  3608. result:=nil;
  3609. { Passing a class type to an "is" expression cannot result in a class
  3610. of that type to be constructed.
  3611. }
  3612. include(right.flags,nf_ignore_for_wpo);
  3613. if is_class(left.resultdef) and
  3614. (right.resultdef.typ=classrefdef) then
  3615. result := ccallnode.createinternres('fpc_do_is',
  3616. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3617. resultdef)
  3618. else
  3619. begin
  3620. if is_class(left.resultdef) then
  3621. if is_shortstring(right.resultdef) then
  3622. procname := 'fpc_class_is_corbaintf'
  3623. else
  3624. procname := 'fpc_class_is_intf'
  3625. else
  3626. if right.resultdef.typ=classrefdef then
  3627. procname := 'fpc_intf_is_class'
  3628. else
  3629. procname := 'fpc_intf_is';
  3630. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3631. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3632. end;
  3633. left := nil;
  3634. right := nil;
  3635. //firstpass(call);
  3636. if codegenerror then
  3637. exit;
  3638. end;
  3639. { dummy pass_2, it will never be called, but we need one since }
  3640. { you can't instantiate an abstract class }
  3641. procedure tisnode.pass_generate_code;
  3642. begin
  3643. end;
  3644. {*****************************************************************************
  3645. TASNODE
  3646. *****************************************************************************}
  3647. constructor tasnode.create(l,r : tnode);
  3648. begin
  3649. inherited create(asn,l,r);
  3650. call := nil;
  3651. end;
  3652. constructor tasnode.create_internal(l,r : tnode);
  3653. begin
  3654. create(l,r);
  3655. include(flags,nf_internal);
  3656. end;
  3657. destructor tasnode.destroy;
  3658. begin
  3659. call.free;
  3660. inherited destroy;
  3661. end;
  3662. function tasnode.dogetcopy: tnode;
  3663. begin
  3664. result := inherited dogetcopy;
  3665. if assigned(call) then
  3666. tasnode(result).call := call.getcopy
  3667. else
  3668. tasnode(result).call := nil;
  3669. end;
  3670. function tasnode.docompare(p: tnode): boolean;
  3671. begin
  3672. result:=
  3673. inherited docompare(p) and
  3674. tasnode(p).call.isequal(call);
  3675. end;
  3676. function tasnode.pass_1 : tnode;
  3677. var
  3678. procname: string;
  3679. begin
  3680. result:=nil;
  3681. { Passing a class type to an "as" expression cannot result in a class
  3682. of that type to be constructed.
  3683. We could put this inside the if-block below, but this way it is
  3684. safe for sure even if the code below changes
  3685. }
  3686. if assigned(right) then
  3687. include(right.flags,nf_ignore_for_wpo);
  3688. if not assigned(call) then
  3689. begin
  3690. if is_class(left.resultdef) and
  3691. (right.resultdef.typ=classrefdef) then
  3692. call := ccallnode.createinternres('fpc_do_as',
  3693. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3694. resultdef)
  3695. else
  3696. begin
  3697. if is_class(left.resultdef) then
  3698. if is_shortstring(right.resultdef) then
  3699. procname := 'fpc_class_as_corbaintf'
  3700. else
  3701. procname := 'fpc_class_as_intf'
  3702. else
  3703. if right.resultdef.typ=classrefdef then
  3704. procname := 'fpc_intf_as_class'
  3705. else
  3706. procname := 'fpc_intf_as';
  3707. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3708. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3709. end;
  3710. left := nil;
  3711. right := nil;
  3712. firstpass(call);
  3713. if codegenerror then
  3714. exit;
  3715. expectloc:=call.expectloc;
  3716. end;
  3717. end;
  3718. end.