ncnv.pas 153 KB

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