ncnv.pas 156 KB

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