ncnv.pas 151 KB

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