ncnv.pas 154 KB

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