ncnv.pas 155 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085
  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,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. { Internal type cast to currency }
  1330. result:=ctypeconvnode.create_internal(result,s64currencytype);
  1331. left:=nil;
  1332. end;
  1333. end;
  1334. function ttypeconvnode.typecheck_real_to_real : tnode;
  1335. begin
  1336. result:=nil;
  1337. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1338. begin
  1339. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1340. include(left.flags,nf_is_currency);
  1341. typecheckpass(left);
  1342. end
  1343. else
  1344. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1345. begin
  1346. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1347. include(left.flags,nf_is_currency);
  1348. typecheckpass(left);
  1349. end;
  1350. end;
  1351. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1352. begin
  1353. result:=nil;
  1354. { handle any constants via cunicodestringtype because the compiler
  1355. cannot convert arbitrary unicodechar constants at compile time to
  1356. a shortstring (since it doesn't know the code page to use) }
  1357. inserttypeconv(left,cunicodestringtype);
  1358. { evaluate again, reset resultdef so the convert_typ
  1359. will be calculated again and cstring_to_pchar will
  1360. be used for futher conversion }
  1361. convtype:=tc_none;
  1362. result:=pass_typecheck;
  1363. end;
  1364. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1365. begin
  1366. result:=nil;
  1367. if is_pwidechar(resultdef) then
  1368. inserttypeconv(left,cunicodestringtype)
  1369. else
  1370. if is_pchar(resultdef) and
  1371. (is_widestring(left.resultdef) or
  1372. is_unicodestring(left.resultdef)) then
  1373. begin
  1374. inserttypeconv(left,getansistringdef);
  1375. { the second pass of second_cstring_to_pchar expects a }
  1376. { strinconstn, but this may become a call to the }
  1377. { widestring manager in case left contains "high ascii" }
  1378. if (left.nodetype<>stringconstn) then
  1379. begin
  1380. result:=left;
  1381. left:=nil;
  1382. end;
  1383. end;
  1384. end;
  1385. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1386. var
  1387. fcc : cardinal;
  1388. pb : pbyte;
  1389. begin
  1390. result:=nil;
  1391. if left.nodetype<>stringconstn then
  1392. internalerror(200510012);
  1393. if tstringconstnode(left).len=4 then
  1394. begin
  1395. pb:=pbyte(tstringconstnode(left).value_str);
  1396. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1397. result:=cordconstnode.create(fcc,u32inttype,false);
  1398. end
  1399. else
  1400. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1401. end;
  1402. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1403. var
  1404. hp : tnode;
  1405. begin
  1406. result:=nil;
  1407. if left.nodetype<>arrayconstructorn then
  1408. internalerror(5546);
  1409. { remove typeconv node }
  1410. hp:=left;
  1411. left:=nil;
  1412. { create a set constructor tree }
  1413. arrayconstructor_to_set(hp);
  1414. result:=hp;
  1415. end;
  1416. function ttypeconvnode.typecheck_set_to_set : tnode;
  1417. begin
  1418. result:=nil;
  1419. { constant sets can be converted by changing the type only }
  1420. if (left.nodetype=setconstn) then
  1421. begin
  1422. left.resultdef:=resultdef;
  1423. result:=left;
  1424. left:=nil;
  1425. exit;
  1426. end;
  1427. end;
  1428. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1429. var
  1430. newblock : tblocknode;
  1431. newstat : tstatementnode;
  1432. restemp : ttempcreatenode;
  1433. begin
  1434. if tstringdef(resultdef).stringtype=st_shortstring then
  1435. begin
  1436. newblock:=internalstatements(newstat);
  1437. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1438. addstatement(newstat,restemp);
  1439. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1440. ctemprefnode.create(restemp),nil))));
  1441. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1442. addstatement(newstat,ctemprefnode.create(restemp));
  1443. result:=newblock;
  1444. end
  1445. else if tstringdef(resultdef).stringtype=st_ansistring then
  1446. result := ccallnode.createinternres(
  1447. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1448. ccallparanode.create(
  1449. cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  1450. ccallparanode.create(left,nil)
  1451. ),
  1452. resultdef
  1453. )
  1454. else
  1455. result := ccallnode.createinternres(
  1456. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1457. ccallparanode.create(left,nil),resultdef);
  1458. left:=nil;
  1459. end;
  1460. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1461. begin
  1462. if assigned(tobjectdef(left.resultdef).iidstr) then
  1463. begin
  1464. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1465. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1466. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1467. tstringconstnode(result).changestringtype(cshortstringtype);
  1468. end
  1469. else
  1470. internalerror(2013112913);
  1471. end;
  1472. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1473. begin
  1474. if assigned(tobjectdef(left.resultdef).iidguid) then
  1475. begin
  1476. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1477. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1478. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1479. end
  1480. else
  1481. internalerror(2013112914);
  1482. end;
  1483. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1484. begin
  1485. { a dynamic array is a pointer to an array, so to convert it to }
  1486. { an open array, we have to dereference it (JM) }
  1487. result := ctypeconvnode.create_internal(left,voidpointertype);
  1488. typecheckpass(result);
  1489. { left is reused }
  1490. left := nil;
  1491. result := cderefnode.create(result);
  1492. include(result.flags,nf_no_checkpointer);
  1493. result.resultdef := resultdef;
  1494. end;
  1495. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1496. var
  1497. newblock : tblocknode;
  1498. newstat : tstatementnode;
  1499. restemp : ttempcreatenode;
  1500. begin
  1501. if tstringdef(resultdef).stringtype=st_shortstring then
  1502. begin
  1503. newblock:=internalstatements(newstat);
  1504. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1505. addstatement(newstat,restemp);
  1506. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1507. ctemprefnode.create(restemp),nil))));
  1508. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1509. addstatement(newstat,ctemprefnode.create(restemp));
  1510. result:=newblock;
  1511. end
  1512. else if tstringdef(resultdef).stringtype=st_ansistring then
  1513. begin
  1514. result:=ccallnode.createinternres(
  1515. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1516. ccallparanode.create(
  1517. cordconstnode.create(
  1518. getparaencoding(resultdef),
  1519. u16inttype,
  1520. true
  1521. ),
  1522. ccallparanode.create(left,nil)
  1523. ),
  1524. resultdef
  1525. );
  1526. end
  1527. else
  1528. result := ccallnode.createinternres(
  1529. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1530. ccallparanode.create(left,nil),resultdef);
  1531. left:=nil;
  1532. end;
  1533. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1534. begin
  1535. result := ccallnode.createinternres(
  1536. 'fpc_variant_to_dynarray',
  1537. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1538. ccallparanode.create(left,nil)
  1539. ),resultdef);
  1540. typecheckpass(result);
  1541. left:=nil;
  1542. end;
  1543. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1544. begin
  1545. result := ccallnode.createinternres(
  1546. 'fpc_dynarray_to_variant',
  1547. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1548. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1549. ),resultdef);
  1550. typecheckpass(result);
  1551. left:=nil;
  1552. end;
  1553. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1554. begin
  1555. if def_is_related(tobjectdef(resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1556. result := ccallnode.createinternres(
  1557. 'fpc_variant_to_idispatch',
  1558. ccallparanode.create(left,nil)
  1559. ,resultdef)
  1560. else
  1561. result := ccallnode.createinternres(
  1562. 'fpc_variant_to_interface',
  1563. ccallparanode.create(left,nil)
  1564. ,resultdef);
  1565. typecheckpass(result);
  1566. left:=nil;
  1567. end;
  1568. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1569. begin
  1570. if def_is_related(tobjectdef(left.resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1571. result := ccallnode.createinternres(
  1572. 'fpc_idispatch_to_variant',
  1573. ccallparanode.create(left,nil)
  1574. ,resultdef)
  1575. else
  1576. result := ccallnode.createinternres(
  1577. 'fpc_interface_to_variant',
  1578. ccallparanode.create(left,nil)
  1579. ,resultdef);
  1580. typecheckpass(result);
  1581. left:=nil;
  1582. end;
  1583. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1584. begin
  1585. result := ctypeconvnode.create_internal(left,sinttype);
  1586. result := ctypeconvnode.create_internal(result,resultdef);
  1587. typecheckpass(result);
  1588. { left is reused }
  1589. left := nil;
  1590. end;
  1591. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1592. begin
  1593. result := ctypeconvnode.create_internal(left,sinttype);
  1594. result := ctypeconvnode.create_internal(result,cvarianttype);
  1595. typecheckpass(result);
  1596. { left is reused }
  1597. left := nil;
  1598. end;
  1599. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1600. var
  1601. newstatement : tstatementnode;
  1602. temp : ttempcreatenode;
  1603. temp2 : ttempcreatenode;
  1604. begin
  1605. { create statements with call to getmem+initialize }
  1606. result:=internalstatements(newstatement);
  1607. { create temp for result }
  1608. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1609. addstatement(newstatement,temp);
  1610. { get temp for array of lengths }
  1611. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1612. addstatement(newstatement,temp2);
  1613. { one dimensional }
  1614. addstatement(newstatement,cassignmentnode.create(
  1615. ctemprefnode.create_offset(temp2,0),
  1616. cordconstnode.create
  1617. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1618. { create call to fpc_dynarr_setlength }
  1619. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1620. ccallparanode.create(caddrnode.create_internal
  1621. (ctemprefnode.create(temp2)),
  1622. ccallparanode.create(cordconstnode.create
  1623. (1,s32inttype,true),
  1624. ccallparanode.create(caddrnode.create_internal
  1625. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1626. ccallparanode.create(
  1627. ctypeconvnode.create_internal(
  1628. ctemprefnode.create(temp),voidpointertype),
  1629. nil))))
  1630. ));
  1631. addstatement(newstatement,ctempdeletenode.create(temp2));
  1632. { copy ... }
  1633. addstatement(newstatement,cassignmentnode.create(
  1634. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1635. left
  1636. ));
  1637. { left is reused }
  1638. left:=nil;
  1639. { the last statement should return the value as
  1640. location and type, this is done be referencing the
  1641. temp and converting it first from a persistent temp to
  1642. normal temp }
  1643. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1644. addstatement(newstatement,ctemprefnode.create(temp));
  1645. end;
  1646. function ttypeconvnode.typecheck_elem_2_openarray : tnode;
  1647. begin
  1648. result:=nil;
  1649. end;
  1650. function ttypeconvnode._typecheck_int_to_int : tnode;
  1651. begin
  1652. result := typecheck_int_to_int;
  1653. end;
  1654. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  1655. begin
  1656. result := typecheck_cord_to_pointer;
  1657. end;
  1658. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  1659. begin
  1660. result := typecheck_chararray_to_string;
  1661. end;
  1662. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  1663. begin
  1664. result := typecheck_string_to_chararray;
  1665. end;
  1666. function ttypeconvnode._typecheck_string_to_string: tnode;
  1667. begin
  1668. result := typecheck_string_to_string;
  1669. end;
  1670. function ttypeconvnode._typecheck_char_to_string : tnode;
  1671. begin
  1672. result := typecheck_char_to_string;
  1673. end;
  1674. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  1675. begin
  1676. result := typecheck_char_to_chararray;
  1677. end;
  1678. function ttypeconvnode._typecheck_int_to_real : tnode;
  1679. begin
  1680. result := typecheck_int_to_real;
  1681. end;
  1682. function ttypeconvnode._typecheck_real_to_real : tnode;
  1683. begin
  1684. result := typecheck_real_to_real;
  1685. end;
  1686. function ttypeconvnode._typecheck_real_to_currency : tnode;
  1687. begin
  1688. result := typecheck_real_to_currency;
  1689. end;
  1690. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  1691. begin
  1692. result := typecheck_cchar_to_pchar;
  1693. end;
  1694. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  1695. begin
  1696. result := typecheck_cstring_to_pchar;
  1697. end;
  1698. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  1699. begin
  1700. result := typecheck_cstring_to_int;
  1701. end;
  1702. function ttypeconvnode._typecheck_char_to_char : tnode;
  1703. begin
  1704. result := typecheck_char_to_char;
  1705. end;
  1706. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  1707. begin
  1708. result := typecheck_arrayconstructor_to_set;
  1709. end;
  1710. function ttypeconvnode._typecheck_set_to_set : tnode;
  1711. begin
  1712. result := typecheck_set_to_set;
  1713. end;
  1714. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  1715. begin
  1716. result := typecheck_pchar_to_string;
  1717. end;
  1718. function ttypeconvnode._typecheck_interface_to_string : tnode;
  1719. begin
  1720. result := typecheck_interface_to_string;
  1721. end;
  1722. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  1723. begin
  1724. result := typecheck_interface_to_guid;
  1725. end;
  1726. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  1727. begin
  1728. result := typecheck_dynarray_to_openarray;
  1729. end;
  1730. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  1731. begin
  1732. result := typecheck_pwchar_to_string;
  1733. end;
  1734. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  1735. begin
  1736. result := typecheck_variant_to_dynarray;
  1737. end;
  1738. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  1739. begin
  1740. result := typecheck_dynarray_to_variant;
  1741. end;
  1742. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  1743. begin
  1744. result := typecheck_variant_to_enum;
  1745. end;
  1746. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  1747. begin
  1748. result := typecheck_enum_to_variant;
  1749. end;
  1750. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  1751. begin
  1752. result := typecheck_proc_to_procvar;
  1753. end;
  1754. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  1755. begin
  1756. result := typecheck_variant_to_interface;
  1757. end;
  1758. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  1759. begin
  1760. result := typecheck_interface_to_variant;
  1761. end;
  1762. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  1763. begin
  1764. result := typecheck_array_2_dynarray;
  1765. end;
  1766. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  1767. begin
  1768. result := typecheck_elem_2_openarray;
  1769. end;
  1770. function ttypeconvnode.target_specific_general_typeconv: boolean;
  1771. begin
  1772. result:=false;
  1773. end;
  1774. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  1775. begin
  1776. result:=false;
  1777. end;
  1778. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1779. var
  1780. pd : tabstractprocdef;
  1781. begin
  1782. result:=nil;
  1783. pd:=tabstractprocdef(left.resultdef);
  1784. { create procvardef (default for create_proc_to_procvar is voiddef,
  1785. but if later a regular inserttypeconvnode() is used to insert a type
  1786. conversion to the actual procvardef, totypedef will be set to the
  1787. real procvartype that we are converting to) }
  1788. if assigned(totypedef) and
  1789. (totypedef.typ=procvardef) then
  1790. resultdef:=totypedef
  1791. else
  1792. begin
  1793. resultdef:=pd.getcopyas(procvardef,pc_normal);
  1794. { only need the address of the method? this is needed
  1795. for @tobject.create. In this case there will be a loadn without
  1796. a methodpointer. }
  1797. if (left.nodetype=loadn) and
  1798. not assigned(tloadnode(left).left) and
  1799. (not(m_nested_procvars in current_settings.modeswitches) or
  1800. not is_nested_pd(tprocvardef(resultdef))) then
  1801. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1802. { calculate parameter list & order }
  1803. tprocvardef(resultdef).calcparas;
  1804. end;
  1805. end;
  1806. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1807. const
  1808. resultdefconvert : array[tconverttype] of pointer = (
  1809. {none} nil,
  1810. {equal} nil,
  1811. {not_possible} nil,
  1812. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  1813. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  1814. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  1815. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  1816. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  1817. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  1818. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  1819. { ansistring_2_pchar } nil,
  1820. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  1821. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  1822. { array_2_pointer } nil,
  1823. { pointer_2_array } nil,
  1824. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  1825. { int_2_bool } nil,
  1826. { bool_2_bool } nil,
  1827. { bool_2_int } nil,
  1828. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  1829. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  1830. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  1831. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  1832. { nil_2_methodprocvar } nil,
  1833. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  1834. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  1835. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  1836. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  1837. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  1838. { class_2_intf } nil,
  1839. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  1840. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  1841. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  1842. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  1843. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  1844. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  1845. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  1846. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  1847. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  1848. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  1849. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray
  1850. );
  1851. type
  1852. tprocedureofobject = function : tnode of object;
  1853. var
  1854. r : packed record
  1855. proc : pointer;
  1856. obj : pointer;
  1857. end;
  1858. begin
  1859. result:=nil;
  1860. { this is a little bit dirty but it works }
  1861. { and should be quite portable too }
  1862. r.proc:=resultdefconvert[c];
  1863. r.obj:=self;
  1864. if assigned(r.proc) then
  1865. result:=tprocedureofobject(r)();
  1866. end;
  1867. function ttypeconvnode.pass_typecheck:tnode;
  1868. var
  1869. hdef : tdef;
  1870. hp : tnode;
  1871. currprocdef : tabstractprocdef;
  1872. aprocdef : tprocdef;
  1873. eq : tequaltype;
  1874. cdoptions : tcompare_defs_options;
  1875. newblock: tblocknode;
  1876. newstatement: tstatementnode;
  1877. tempnode: ttempcreatenode;
  1878. begin
  1879. result:=nil;
  1880. resultdef:=totypedef;
  1881. typecheckpass(left);
  1882. if codegenerror then
  1883. exit;
  1884. { When absolute force tc_equal }
  1885. if (nf_absolute in flags) then
  1886. begin
  1887. convtype:=tc_equal;
  1888. if (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  1889. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) then
  1890. make_not_regable(left,[ra_addr_regable]);
  1891. exit;
  1892. end;
  1893. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1894. convert on the procvar value. This is used to access the
  1895. fields of a methodpointer }
  1896. if not(nf_load_procvar in flags) and
  1897. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1898. maybe_call_procvar(left,true);
  1899. { convert array constructors to sets, because there is no conversion
  1900. possible for array constructors }
  1901. if (resultdef.typ<>arraydef) and
  1902. is_array_constructor(left.resultdef) then
  1903. begin
  1904. arrayconstructor_to_set(left);
  1905. typecheckpass(left);
  1906. end;
  1907. if target_specific_general_typeconv then
  1908. exit;
  1909. if convtype=tc_none then
  1910. begin
  1911. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  1912. { overloaded operators require calls, which is not possible inside
  1913. a constant declaration }
  1914. if (block_type<>bt_const) and
  1915. not(nf_internal in flags) then
  1916. include(cdoptions,cdo_check_operator);
  1917. if nf_explicit in flags then
  1918. include(cdoptions,cdo_explicit);
  1919. if nf_internal in flags then
  1920. include(cdoptions,cdo_internal);
  1921. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1922. case eq of
  1923. te_exact,
  1924. te_equal :
  1925. begin
  1926. result := simplify(false);
  1927. if assigned(result) then
  1928. exit;
  1929. { in case of bitpacked accesses, the original type must
  1930. remain so that not too many/few bits are laoded }
  1931. if is_bitpacked_access(left) then
  1932. convtype:=tc_int_2_int;
  1933. { Only leave when there is no conversion to do.
  1934. We can still need to call a conversion routine,
  1935. like the routine to convert a stringconstnode }
  1936. if (convtype in [tc_equal,tc_not_possible]) and
  1937. { some conversions, like dynarray to pointer in Delphi
  1938. mode, must not be removed, because then we get memory
  1939. leaks due to missing temp finalization }
  1940. (not is_managed_type(left.resultdef) or
  1941. { different kinds of refcounted types may need calls
  1942. to different kinds of refcounting helpers }
  1943. (resultdef=left.resultdef)) then
  1944. begin
  1945. left.resultdef:=resultdef;
  1946. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1947. include(left.flags, nf_typedaddr);
  1948. result:=left;
  1949. left:=nil;
  1950. exit;
  1951. end;
  1952. end;
  1953. te_convert_l1,
  1954. te_convert_l2,
  1955. te_convert_l3,
  1956. te_convert_l4,
  1957. te_convert_l5,
  1958. te_convert_l6:
  1959. { nothing to do }
  1960. ;
  1961. te_convert_operator :
  1962. begin
  1963. include(current_procinfo.flags,pi_do_call);
  1964. addsymref(aprocdef.procsym);
  1965. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1966. { tell explicitly which def we must use !! (PM) }
  1967. tcallnode(hp).procdefinition:=aprocdef;
  1968. left:=nil;
  1969. result:=hp;
  1970. exit;
  1971. end;
  1972. te_incompatible :
  1973. begin
  1974. { Procedures have a resultdef of voiddef and functions of their
  1975. own resultdef. They will therefore always be incompatible with
  1976. a procvar. Because isconvertable cannot check for procedures we
  1977. use an extra check for them.}
  1978. if (left.nodetype=calln) and
  1979. (tcallnode(left).required_para_count=0) and
  1980. (resultdef.typ=procvardef) and
  1981. (
  1982. (m_tp_procvar in current_settings.modeswitches) or
  1983. (m_mac_procvar in current_settings.modeswitches)
  1984. ) then
  1985. begin
  1986. if assigned(tcallnode(left).right) then
  1987. begin
  1988. { this is already a procvar, if it is really equal
  1989. is checked below }
  1990. convtype:=tc_equal;
  1991. hp:=tcallnode(left).right.getcopy;
  1992. currprocdef:=tabstractprocdef(hp.resultdef);
  1993. end
  1994. else
  1995. begin
  1996. convtype:=tc_proc_2_procvar;
  1997. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1998. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1999. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2000. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2001. begin
  2002. if assigned(tcallnode(left).methodpointer) then
  2003. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  2004. else
  2005. tloadnode(hp).set_mp(load_self_node);
  2006. end;
  2007. typecheckpass(hp);
  2008. end;
  2009. left.free;
  2010. left:=hp;
  2011. { Now check if the procedure we are going to assign to
  2012. the procvar, is compatible with the procvar's type }
  2013. if not(nf_explicit in flags) and
  2014. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
  2015. IncompatibleTypes(left.resultdef,resultdef);
  2016. exit;
  2017. end
  2018. else if maybe_global_proc_to_nested(left,resultdef) then
  2019. begin
  2020. result:=left;
  2021. left:=nil;
  2022. exit;
  2023. end;
  2024. { Handle explicit type conversions }
  2025. if nf_explicit in flags then
  2026. begin
  2027. { do common tc_equal cast, except when dealing with proc -> procvar
  2028. (may have to get rid of method pointer) }
  2029. if (left.resultdef.typ<>procdef) or
  2030. (resultdef.typ<>procvardef) then
  2031. convtype:=tc_equal
  2032. else
  2033. convtype:=tc_proc_2_procvar;
  2034. { ordinal constants can be resized to 1,2,4,8 bytes }
  2035. if (left.nodetype=ordconstn) then
  2036. begin
  2037. { Insert typeconv for ordinal to the correct size first on left, after
  2038. that the other conversion can be done }
  2039. hdef:=nil;
  2040. case longint(resultdef.size) of
  2041. 1 :
  2042. hdef:=s8inttype;
  2043. 2 :
  2044. hdef:=s16inttype;
  2045. 4 :
  2046. hdef:=s32inttype;
  2047. 8 :
  2048. hdef:=s64inttype;
  2049. end;
  2050. { we need explicit, because it can also be an enum }
  2051. if assigned(hdef) then
  2052. inserttypeconv_internal(left,hdef)
  2053. else
  2054. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2055. end;
  2056. { class/interface to class/interface, with checkobject support }
  2057. if is_class_or_interface_or_objc(resultdef) and
  2058. is_class_or_interface_or_objc(left.resultdef) then
  2059. begin
  2060. { check if the types are related }
  2061. if not(nf_internal in flags) and
  2062. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2063. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2064. begin
  2065. { Give an error when typecasting class to interface, this is compatible
  2066. with delphi }
  2067. if is_interface(resultdef) and
  2068. not is_interface(left.resultdef) then
  2069. CGMessage2(type_e_classes_not_related,
  2070. FullTypeName(left.resultdef,resultdef),
  2071. FullTypeName(resultdef,left.resultdef))
  2072. else
  2073. CGMessage2(type_w_classes_not_related,
  2074. FullTypeName(left.resultdef,resultdef),
  2075. FullTypeName(resultdef,left.resultdef))
  2076. end;
  2077. { Add runtime check? }
  2078. if not is_objc_class_or_protocol(resultdef) and
  2079. not is_objc_class_or_protocol(left.resultdef) and
  2080. (cs_check_object in current_settings.localswitches) and
  2081. not(nf_internal in flags) then
  2082. begin
  2083. { we can translate the typeconvnode to 'as' when
  2084. typecasting to a class or interface }
  2085. { we need to make sure the result can still be
  2086. passed as a var parameter }
  2087. newblock:=internalstatements(newstatement);
  2088. if (valid_for_var(left,false)) then
  2089. begin
  2090. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2091. addstatement(newstatement,tempnode);
  2092. addstatement(newstatement,cassignmentnode.create(
  2093. ctemprefnode.create(tempnode),
  2094. caddrnode.create_internal(left)));
  2095. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2096. end
  2097. else
  2098. begin
  2099. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2100. addstatement(newstatement,tempnode);
  2101. addstatement(newstatement,cassignmentnode.create(
  2102. ctemprefnode.create(tempnode),
  2103. left));
  2104. left:=ctemprefnode.create(tempnode);
  2105. end;
  2106. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2107. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2108. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2109. left:=nil;
  2110. result:=newblock;
  2111. exit;
  2112. end;
  2113. end
  2114. else
  2115. begin
  2116. { only if the same size or formal def, and }
  2117. { don't allow type casting of constants to }
  2118. { structured types }
  2119. if not(
  2120. (left.resultdef.typ=formaldef) or
  2121. {$ifdef jvm}
  2122. { enums /are/ class instances on the JVM
  2123. platform }
  2124. (((left.resultdef.typ=enumdef) and
  2125. (resultdef.typ=objectdef)) or
  2126. ((resultdef.typ=enumdef) and
  2127. (left.resultdef.typ=objectdef))) or
  2128. {$endif}
  2129. (
  2130. not(is_open_array(left.resultdef)) and
  2131. not(is_array_constructor(left.resultdef)) and
  2132. not(is_array_of_const(left.resultdef)) and
  2133. (left.resultdef.size=resultdef.size) and
  2134. { disallow casts of const nodes }
  2135. (not is_constnode(left) or
  2136. { however, there are some exceptions }
  2137. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2138. filedef,variantdef,objectdef]) or
  2139. is_class_or_interface_or_objc(resultdef) or
  2140. { the softfloat code generates casts <const. float> to record }
  2141. (nf_internal in flags)
  2142. ))
  2143. ) or
  2144. (
  2145. is_void(left.resultdef) and
  2146. (left.nodetype=derefn)
  2147. )
  2148. ) then
  2149. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2150. else
  2151. begin
  2152. { perform target-specific explicit typecast
  2153. checks }
  2154. if target_specific_explicit_typeconv then
  2155. begin
  2156. result:=simplify(false);
  2157. exit;
  2158. end;
  2159. end;
  2160. end;
  2161. end
  2162. else
  2163. IncompatibleTypes(left.resultdef,resultdef);
  2164. end;
  2165. else
  2166. internalerror(200211231);
  2167. end;
  2168. end;
  2169. { Give hint or warning for unportable code, exceptions are
  2170. - typecasts from constants
  2171. - void }
  2172. if not(nf_internal in flags) and
  2173. (left.nodetype<>ordconstn) and
  2174. not(is_void(left.resultdef)) and
  2175. (((left.resultdef.typ=orddef) and
  2176. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2177. ((resultdef.typ=orddef) and
  2178. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2179. begin
  2180. {Converting pointers to signed integers is a bad idea. Warn.}
  2181. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2182. { Give a warning when sizes don't match, because then info will be lost }
  2183. if left.resultdef.size=resultdef.size then
  2184. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2185. else
  2186. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2187. end;
  2188. { tc_cord_2_pointer still requires a type check, which
  2189. simplify does not do }
  2190. if (convtype<>tc_cord_2_pointer) then
  2191. begin
  2192. result := simplify(false);
  2193. if assigned(result) then
  2194. exit;
  2195. end;
  2196. { now call the resultdef helper to do constant folding }
  2197. result:=typecheck_call_helper(convtype);
  2198. end;
  2199. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2200. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2201. this applies is powerpc64
  2202. }
  2203. {$ifndef CPUNO32BITOPS}
  2204. { checks whether we can safely remove 64 bit typeconversions
  2205. in case range and overflow checking are off, and in case
  2206. the result of this node tree is downcasted again to a
  2207. 8/16/32 bit value afterwards
  2208. We do this on 64 bit CPUs as well, they benefit from it as well }
  2209. function checkremove64bittypeconvs(n: tnode; out gotsint: boolean): boolean;
  2210. var
  2211. gotdivmod: boolean;
  2212. { checks whether a node is either an u32bit, or originally }
  2213. { was one but was implicitly converted to s64bit }
  2214. function wasoriginallyint32(n: tnode): boolean;
  2215. begin
  2216. if (n.resultdef.typ<>orddef) then
  2217. exit(false);
  2218. if (torddef(n.resultdef).ordtype in [s8bit,u8bit,s16bit,u16bit,s32bit,u32bit]) then
  2219. begin
  2220. if (torddef(n.resultdef).ordtype in [s8bit,s16bit,s32bit]) then
  2221. gotsint:=true;
  2222. exit(true);
  2223. end;
  2224. if (torddef(n.resultdef).ordtype in [u64bit,s64bit]) and
  2225. { nf_explicit is also set for explicitly typecasted }
  2226. { ordconstn's }
  2227. ([nf_internal,nf_explicit]*n.flags=[]) and
  2228. { either a typeconversion node coming from u32bit }
  2229. (((n.nodetype=typeconvn) and
  2230. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2231. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s8bit,u8bit,s16bit,u16bit,s32bit,u32bit])) or
  2232. { or an ordconstnode which was/is a valid cardinal }
  2233. ((n.nodetype=ordconstn) and
  2234. (tordconstnode(n).value>=int64(low(longint))) and
  2235. (tordconstnode(n).value<=high(cardinal)))) then
  2236. begin
  2237. if ((n.nodetype=typeconvn) and
  2238. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s8bit,s16bit,s32bit])) or
  2239. ((n.nodetype=ordconstn) and
  2240. (tordconstnode(n).value<0)) then
  2241. gotsint:=true;
  2242. exit(true);
  2243. end;
  2244. result:=false;
  2245. end;
  2246. function docheckremove64bittypeconvs(n: tnode): boolean;
  2247. begin
  2248. result:=false;
  2249. if wasoriginallyint32(n) then
  2250. exit(true);
  2251. case n.nodetype of
  2252. subn,orn,xorn:
  2253. begin
  2254. { the result could become negative in this case }
  2255. if n.nodetype=subn then
  2256. gotsint:=true;
  2257. result:=
  2258. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2259. docheckremove64bittypeconvs(tbinarynode(n).right);
  2260. end;
  2261. addn,muln,divn,modn,andn:
  2262. begin
  2263. if n.nodetype in [divn,modn] then
  2264. gotdivmod:=true;
  2265. result:=
  2266. (docheckremove64bittypeconvs(tbinarynode(n).left) and
  2267. docheckremove64bittypeconvs(tbinarynode(n).right)) or
  2268. { in case of div/mod, the result of that division/modulo can
  2269. usually be different in 32 and 64 bit }
  2270. (not gotdivmod and
  2271. (((n.nodetype=andn) and wasoriginallyint32(tbinarynode(n).left)) or
  2272. ((n.nodetype=andn) and wasoriginallyint32(tbinarynode(n).right))));
  2273. end;
  2274. end;
  2275. end;
  2276. begin { checkremove64bittypeconvs }
  2277. gotdivmod:=false;
  2278. gotsint:=false;
  2279. result:=
  2280. docheckremove64bittypeconvs(n) and
  2281. not(gotdivmod and gotsint);
  2282. end;
  2283. procedure doremove64bittypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean);
  2284. begin
  2285. case n.nodetype of
  2286. subn,addn,muln,divn,modn,xorn,andn,orn:
  2287. begin
  2288. exclude(n.flags,nf_internal);
  2289. if not forceunsigned and
  2290. is_signed(n.resultdef) then
  2291. begin
  2292. doremove64bittypeconvs(tbinarynode(n).left,s32inttype,false);
  2293. doremove64bittypeconvs(tbinarynode(n).right,s32inttype,false);
  2294. n.resultdef:=s32inttype
  2295. end
  2296. else
  2297. begin
  2298. doremove64bittypeconvs(tbinarynode(n).left,u32inttype,forceunsigned);
  2299. doremove64bittypeconvs(tbinarynode(n).right,u32inttype,forceunsigned);
  2300. n.resultdef:=u32inttype
  2301. end;
  2302. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  2303. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  2304. // ) then
  2305. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  2306. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  2307. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  2308. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  2309. end;
  2310. typeconvn:
  2311. begin
  2312. ttypeconvnode(n).totypedef:=todef;
  2313. { may change the type conversion, e.g. if the old conversion was
  2314. from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
  2315. n.resultdef:=nil;
  2316. ttypeconvnode(n).convtype:=tc_none;
  2317. typecheckpass(n);
  2318. end;
  2319. else
  2320. inserttypeconv_internal(n,todef);
  2321. end;
  2322. end;
  2323. {$endif not CPUNO32BITOPS}
  2324. procedure swap_const_value (var val : TConstExprInt; size : longint);
  2325. begin
  2326. case size of
  2327. 1 : {do nothing };
  2328. 2 : if val.signed then
  2329. val.svalue:=swapendian(smallint(val.svalue))
  2330. else
  2331. val.uvalue:=swapendian(word(val.uvalue));
  2332. 4 : if val.signed then
  2333. val.svalue:=swapendian(longint(val.svalue))
  2334. else
  2335. val.uvalue:=swapendian(qword(val.uvalue));
  2336. 8 : if val.signed then
  2337. val.svalue:=swapendian(int64(val.svalue))
  2338. else
  2339. val.uvalue:=swapendian(qword(val.uvalue));
  2340. else
  2341. internalerror(2014111201);
  2342. end;
  2343. end;
  2344. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2345. var
  2346. hp: tnode;
  2347. {$ifndef CPUNO32BITOPS}
  2348. foundsint: boolean;
  2349. {$endif not CPUNO32BITOPS}
  2350. begin
  2351. result := nil;
  2352. { Constant folding and other node transitions to
  2353. remove the typeconv node }
  2354. case left.nodetype of
  2355. stringconstn :
  2356. if (convtype=tc_string_2_string) and
  2357. (resultdef.typ=stringdef) and
  2358. (
  2359. ((not is_widechararray(left.resultdef) and
  2360. not is_wide_or_unicode_string(left.resultdef)) or
  2361. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  2362. )
  2363. ) then
  2364. begin
  2365. { output string consts in local ansistring encoding }
  2366. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0)or(tstringdef(resultdef).encoding=globals.CP_NONE)) then
  2367. tstringconstnode(left).changestringtype(getansistringdef)
  2368. else
  2369. tstringconstnode(left).changestringtype(resultdef);
  2370. result:=left;
  2371. resultdef:=left.resultdef;
  2372. left:=nil;
  2373. exit;
  2374. end;
  2375. realconstn :
  2376. begin
  2377. if (convtype = tc_real_2_currency) then
  2378. result := typecheck_real_to_currency
  2379. else if (convtype = tc_real_2_real) then
  2380. result := typecheck_real_to_real
  2381. else
  2382. exit;
  2383. if not(assigned(result)) then
  2384. begin
  2385. result := left;
  2386. left := nil;
  2387. end;
  2388. if (result.nodetype = realconstn) then
  2389. begin
  2390. hp:=result;
  2391. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2392. if ([nf_explicit,nf_internal] * flags <> []) then
  2393. include(result.flags, nf_explicit);
  2394. hp.free;
  2395. end;
  2396. end;
  2397. niln :
  2398. begin
  2399. { nil to ordinal node }
  2400. if (resultdef.typ=orddef) then
  2401. begin
  2402. hp:=cordconstnode.create(0,resultdef,true);
  2403. if ([nf_explicit,nf_internal] * flags <> []) then
  2404. include(hp.flags, nf_explicit);
  2405. result:=hp;
  2406. exit;
  2407. end
  2408. else
  2409. { fold nil to any pointer type }
  2410. if (resultdef.typ=pointerdef) then
  2411. begin
  2412. hp:=cnilnode.create;
  2413. hp.resultdef:=resultdef;
  2414. if ([nf_explicit,nf_internal] * flags <> []) then
  2415. include(hp.flags, nf_explicit);
  2416. result:=hp;
  2417. exit;
  2418. end
  2419. else
  2420. { remove typeconv after niln, but not when the result is a
  2421. methodpointer. The typeconv of the methodpointer will then
  2422. take care of updateing size of niln to OS_64 }
  2423. if not((resultdef.typ=procvardef) and
  2424. not(tprocvardef(resultdef).is_addressonly)) then
  2425. begin
  2426. left.resultdef:=resultdef;
  2427. if ([nf_explicit,nf_internal] * flags <> []) then
  2428. include(left.flags, nf_explicit);
  2429. result:=left;
  2430. left:=nil;
  2431. exit;
  2432. end;
  2433. end;
  2434. ordconstn :
  2435. begin
  2436. { ordinal contants can be directly converted }
  2437. { but not char to char because it is a widechar to char or via versa }
  2438. { which needs extra code to do the code page transistion }
  2439. { constant ordinal to pointer }
  2440. if (resultdef.typ=pointerdef) and
  2441. (convtype<>tc_cchar_2_pchar) then
  2442. begin
  2443. if (target_info.system in systems_managed_vm) and
  2444. (tordconstnode(left).value<>0) then
  2445. message(parser_e_feature_unsupported_for_vm);
  2446. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2447. if ([nf_explicit,nf_internal] * flags <> []) then
  2448. include(hp.flags, nf_explicit);
  2449. result:=hp;
  2450. exit;
  2451. end
  2452. else if is_ordinal(resultdef) and
  2453. not(convtype=tc_char_2_char) then
  2454. begin
  2455. { replace the resultdef and recheck the range }
  2456. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  2457. include(left.flags, nf_explicit)
  2458. else
  2459. { no longer an ordconst with an explicit typecast }
  2460. exclude(left.flags, nf_explicit);
  2461. { when converting from one boolean type to another, force }
  2462. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2463. { (Delphi-compatibile) }
  2464. if is_boolean(left.resultdef) and
  2465. is_boolean(resultdef) and
  2466. (is_cbool(left.resultdef) or
  2467. is_cbool(resultdef)) then
  2468. begin
  2469. if is_pasbool(resultdef) then
  2470. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2471. else
  2472. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2473. end
  2474. else
  2475. begin
  2476. { for constant values on absolute variables, swaping is required }
  2477. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2478. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  2479. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags)
  2480. or (nf_absolute in flags),false);
  2481. { swap value back, but according to new type }
  2482. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2483. swap_const_value(tordconstnode(left).value,resultdef.size);
  2484. end;
  2485. left.resultdef:=resultdef;
  2486. tordconstnode(left).typedef:=resultdef;
  2487. if is_signed(resultdef) then
  2488. tordconstnode(left).value.signed:=true
  2489. else
  2490. tordconstnode(left).value.signed:=false;
  2491. result:=left;
  2492. left:=nil;
  2493. exit;
  2494. end;
  2495. end;
  2496. pointerconstn :
  2497. begin
  2498. { pointerconstn to any pointer is folded too }
  2499. if (resultdef.typ=pointerdef) then
  2500. begin
  2501. left.resultdef:=resultdef;
  2502. if ([nf_explicit,nf_internal] * flags <> []) then
  2503. include(left.flags, nf_explicit)
  2504. else
  2505. { no longer an ordconst with an explicit typecast }
  2506. exclude(left.flags, nf_explicit);
  2507. result:=left;
  2508. left:=nil;
  2509. exit;
  2510. end
  2511. { constant pointer to ordinal }
  2512. else if is_ordinal(resultdef) then
  2513. begin
  2514. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2515. resultdef,not(nf_explicit in flags));
  2516. if ([nf_explicit,nf_internal] * flags <> []) then
  2517. include(hp.flags, nf_explicit);
  2518. result:=hp;
  2519. exit;
  2520. end;
  2521. end;
  2522. end;
  2523. {$ifndef CPUNO32BITOPS}
  2524. { must be done before code below, because we need the
  2525. typeconversions for ordconstn's as well }
  2526. case convtype of
  2527. tc_int_2_int:
  2528. begin
  2529. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2530. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2531. (resultdef.size <= 4) and
  2532. is_64bitint(left.resultdef) and
  2533. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2534. checkremove64bittypeconvs(left,foundsint) then
  2535. begin
  2536. { avoid unnecessary widening of intermediary calculations }
  2537. { to 64 bit }
  2538. doremove64bittypeconvs(left,generrordef,not foundsint);
  2539. end;
  2540. end;
  2541. end;
  2542. {$endif not CPUNO32BITOPS}
  2543. end;
  2544. procedure Ttypeconvnode.mark_write;
  2545. begin
  2546. left.mark_write;
  2547. end;
  2548. function ttypeconvnode.first_cord_to_pointer : tnode;
  2549. begin
  2550. result:=nil;
  2551. internalerror(200104043);
  2552. end;
  2553. function ttypeconvnode.first_int_to_int : tnode;
  2554. begin
  2555. first_int_to_int:=nil;
  2556. expectloc:=left.expectloc;
  2557. if not is_void(left.resultdef) then
  2558. begin
  2559. if (left.expectloc<>LOC_REGISTER) and
  2560. ((resultdef.size>left.resultdef.size) or
  2561. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2562. expectloc:=LOC_REGISTER
  2563. else
  2564. if (left.expectloc=LOC_CREGISTER) and
  2565. (resultdef.size<left.resultdef.size) then
  2566. expectloc:=LOC_REGISTER;
  2567. end;
  2568. end;
  2569. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2570. begin
  2571. result:=nil;
  2572. expectloc:=LOC_REGISTER;
  2573. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2574. if (cs_create_pic in current_settings.moduleswitches) then
  2575. include(current_procinfo.flags,pi_needs_got);
  2576. end;
  2577. function ttypeconvnode.first_cstring_to_int : tnode;
  2578. begin
  2579. result:=nil;
  2580. internalerror(200510014);
  2581. end;
  2582. function ttypeconvnode.first_string_to_chararray : tnode;
  2583. begin
  2584. first_string_to_chararray:=nil;
  2585. expectloc:=left.expectloc;
  2586. end;
  2587. function ttypeconvnode.first_char_to_string : tnode;
  2588. begin
  2589. first_char_to_string:=nil;
  2590. if tstringdef(resultdef).stringtype=st_shortstring then
  2591. inc(current_procinfo.estimatedtempsize,256);
  2592. expectloc:=LOC_REFERENCE;
  2593. end;
  2594. function ttypeconvnode.first_char_to_chararray : tnode;
  2595. begin
  2596. if resultdef.size <> 1 then
  2597. begin
  2598. { convert first to string, then to chararray }
  2599. inserttypeconv(left,cshortstringtype);
  2600. inserttypeconv(left,resultdef);
  2601. result:=left;
  2602. left := nil;
  2603. exit;
  2604. end;
  2605. result := nil;
  2606. end;
  2607. function ttypeconvnode.first_nothing : tnode;
  2608. begin
  2609. first_nothing:=nil;
  2610. end;
  2611. function ttypeconvnode.first_array_to_pointer : tnode;
  2612. begin
  2613. first_array_to_pointer:=nil;
  2614. expectloc:=LOC_REGISTER;
  2615. end;
  2616. function ttypeconvnode.first_int_to_real: tnode;
  2617. var
  2618. fname: string[32];
  2619. begin
  2620. if target_info.system in systems_wince then
  2621. begin
  2622. { converting a 64bit integer to a float requires a helper }
  2623. if is_64bitint(left.resultdef) or
  2624. is_currency(left.resultdef) then
  2625. begin
  2626. { hack to avoid double division by 10000, as it's
  2627. already done by typecheckpass.resultdef_int_to_real }
  2628. if is_currency(left.resultdef) then
  2629. left.resultdef := s64inttype;
  2630. if is_signed(left.resultdef) then
  2631. fname:='I64TO'
  2632. else
  2633. fname:='UI64TO';
  2634. end
  2635. else
  2636. { other integers are supposed to be 32 bit }
  2637. begin
  2638. if is_signed(left.resultdef) then
  2639. fname:='ITO'
  2640. else
  2641. fname:='UTO';
  2642. firstpass(left);
  2643. end;
  2644. if tfloatdef(resultdef).floattype=s64real then
  2645. fname:=fname+'D'
  2646. else
  2647. fname:=fname+'S';
  2648. result:=ccallnode.createintern(fname,ccallparanode.create(
  2649. left,nil));
  2650. left:=nil;
  2651. firstpass(result);
  2652. exit;
  2653. end
  2654. else
  2655. begin
  2656. { converting a 64bit integer to a float requires a helper }
  2657. if is_64bitint(left.resultdef) or
  2658. is_currency(left.resultdef) then
  2659. begin
  2660. { hack to avoid double division by 10000, as it's
  2661. already done by typecheckpass.resultdef_int_to_real }
  2662. if is_currency(left.resultdef) then
  2663. left.resultdef := s64inttype;
  2664. if is_signed(left.resultdef) then
  2665. fname:='int64_to_'
  2666. else
  2667. { we can't do better currently }
  2668. fname:='qword_to_';
  2669. end
  2670. else
  2671. { other integers are supposed to be 32 bit }
  2672. begin
  2673. if is_signed(left.resultdef) then
  2674. fname:='int32_to_'
  2675. else
  2676. fname:='int64_to_';
  2677. firstpass(left);
  2678. end;
  2679. if tfloatdef(resultdef).floattype=s64real then
  2680. fname:=fname+'float64'
  2681. else
  2682. fname:=fname+'float32';
  2683. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2684. left,nil)),resultdef);
  2685. left:=nil;
  2686. firstpass(result);
  2687. exit;
  2688. end;
  2689. end;
  2690. function ttypeconvnode.first_real_to_real : tnode;
  2691. begin
  2692. {$ifdef cpufpemu}
  2693. if cs_fp_emulation in current_settings.moduleswitches then
  2694. begin
  2695. if target_info.system in systems_wince then
  2696. begin
  2697. case tfloatdef(left.resultdef).floattype of
  2698. s32real:
  2699. case tfloatdef(resultdef).floattype of
  2700. s64real:
  2701. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2702. s32real:
  2703. begin
  2704. result:=left;
  2705. left:=nil;
  2706. end;
  2707. else
  2708. internalerror(2005082704);
  2709. end;
  2710. s64real:
  2711. case tfloatdef(resultdef).floattype of
  2712. s32real:
  2713. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2714. s64real:
  2715. begin
  2716. result:=left;
  2717. left:=nil;
  2718. end;
  2719. else
  2720. internalerror(2005082703);
  2721. end;
  2722. else
  2723. internalerror(2005082702);
  2724. end;
  2725. left:=nil;
  2726. firstpass(result);
  2727. exit;
  2728. end
  2729. else
  2730. begin
  2731. case tfloatdef(left.resultdef).floattype of
  2732. s32real:
  2733. case tfloatdef(resultdef).floattype of
  2734. s64real:
  2735. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2736. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2737. s32real:
  2738. begin
  2739. result:=left;
  2740. left:=nil;
  2741. end;
  2742. else
  2743. internalerror(200610151);
  2744. end;
  2745. s64real:
  2746. case tfloatdef(resultdef).floattype of
  2747. s32real:
  2748. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2749. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2750. s64real:
  2751. begin
  2752. result:=left;
  2753. left:=nil;
  2754. end;
  2755. else
  2756. internalerror(200610152);
  2757. end;
  2758. else
  2759. internalerror(200610153);
  2760. end;
  2761. left:=nil;
  2762. firstpass(result);
  2763. exit;
  2764. end;
  2765. end
  2766. else
  2767. {$endif cpufpemu}
  2768. begin
  2769. first_real_to_real:=nil;
  2770. if not use_vectorfpu(resultdef) then
  2771. expectloc:=LOC_FPUREGISTER
  2772. else
  2773. expectloc:=LOC_MMREGISTER;
  2774. end;
  2775. end;
  2776. function ttypeconvnode.first_pointer_to_array : tnode;
  2777. begin
  2778. first_pointer_to_array:=nil;
  2779. expectloc:=LOC_REFERENCE;
  2780. end;
  2781. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2782. begin
  2783. first_cchar_to_pchar:=nil;
  2784. internalerror(200104021);
  2785. end;
  2786. function ttypeconvnode.first_bool_to_int : tnode;
  2787. begin
  2788. first_bool_to_int:=nil;
  2789. { byte(boolean) or word(wordbool) or longint(longbool) must
  2790. be accepted for var parameters }
  2791. if (nf_explicit in flags) and
  2792. (left.resultdef.size=resultdef.size) and
  2793. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2794. exit;
  2795. expectloc:=LOC_REGISTER;
  2796. end;
  2797. function ttypeconvnode.first_int_to_bool : tnode;
  2798. begin
  2799. first_int_to_bool:=nil;
  2800. { byte(boolean) or word(wordbool) or longint(longbool) must
  2801. be accepted for var parameters }
  2802. if (nf_explicit in flags) and
  2803. (left.resultdef.size=resultdef.size) and
  2804. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2805. exit;
  2806. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2807. { convert to a boolean (only necessary for 32bit processors) }
  2808. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2809. and is_cbool(resultdef) then
  2810. begin
  2811. left:=ctypeconvnode.create_internal(left,s32inttype);
  2812. firstpass(left);
  2813. exit;
  2814. end;
  2815. expectloc:=LOC_REGISTER;
  2816. end;
  2817. function ttypeconvnode.first_bool_to_bool : tnode;
  2818. begin
  2819. first_bool_to_bool:=nil;
  2820. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  2821. not is_cbool(resultdef) then
  2822. expectloc := left.expectloc
  2823. else
  2824. expectloc:=LOC_REGISTER;
  2825. end;
  2826. function ttypeconvnode.first_char_to_char : tnode;
  2827. var
  2828. fname: string[18];
  2829. begin
  2830. if (torddef(resultdef).ordtype=uchar) and
  2831. (torddef(left.resultdef).ordtype=uwidechar) then
  2832. fname := 'fpc_uchar_to_char'
  2833. else if (torddef(resultdef).ordtype=uwidechar) and
  2834. (torddef(left.resultdef).ordtype=uchar) then
  2835. fname := 'fpc_char_to_uchar'
  2836. else
  2837. internalerror(2007081201);
  2838. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2839. left:=nil;
  2840. firstpass(result);
  2841. end;
  2842. function ttypeconvnode.first_proc_to_procvar : tnode;
  2843. begin
  2844. first_proc_to_procvar:=nil;
  2845. { if we take the address of a nested function, the current function/
  2846. procedure needs a stack frame since it's required to construct
  2847. the nested procvar }
  2848. if is_nested_pd(tprocvardef(resultdef)) then
  2849. include(current_procinfo.flags,pi_needs_stackframe);
  2850. if tabstractprocdef(resultdef).is_addressonly then
  2851. expectloc:=LOC_REGISTER
  2852. else
  2853. expectloc:=left.expectloc;
  2854. end;
  2855. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2856. begin
  2857. first_nil_to_methodprocvar:=nil;
  2858. expectloc:=LOC_REGISTER;
  2859. end;
  2860. function ttypeconvnode.first_set_to_set : tnode;
  2861. var
  2862. newstatement : tstatementnode;
  2863. temp : ttempcreatenode;
  2864. begin
  2865. { in theory, we should do range checking here,
  2866. but Delphi doesn't do it either (FK) }
  2867. if left.nodetype=setconstn then
  2868. begin
  2869. left.resultdef:=resultdef;
  2870. result:=left;
  2871. left:=nil;
  2872. end
  2873. { equal sets for the code generator? }
  2874. else if (left.resultdef.size=resultdef.size) and
  2875. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2876. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2877. { e.g., this prints true because of this:
  2878. var
  2879. sa: set of 1..2;
  2880. sb: set of 5..6;
  2881. b: byte;
  2882. begin
  2883. b:=1;
  2884. sa:=[1..2];
  2885. sb:=sa;
  2886. writeln(b in sb);
  2887. end.
  2888. }
  2889. begin
  2890. result:=left;
  2891. left:=nil;
  2892. end
  2893. else
  2894. begin
  2895. result:=internalstatements(newstatement);
  2896. { in case left is a smallset expression, it can be an addn or so. }
  2897. { fpc_varset_load expects a formal const parameter, which doesn't }
  2898. { accept set addn's -> assign to a temp first and pass the temp }
  2899. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2900. begin
  2901. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2902. addstatement(newstatement,temp);
  2903. { temp := left }
  2904. addstatement(newstatement,cassignmentnode.create(
  2905. ctemprefnode.create(temp),left));
  2906. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2907. addstatement(newstatement,ctemprefnode.create(temp));
  2908. left:=result;
  2909. firstpass(left);
  2910. { recreate the result's internalstatements list }
  2911. result:=internalstatements(newstatement);
  2912. end;
  2913. { create temp for result }
  2914. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2915. addstatement(newstatement,temp);
  2916. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2917. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2918. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2919. ccallparanode.create(ctemprefnode.create(temp),
  2920. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2921. ccallparanode.create(left,nil))))))
  2922. );
  2923. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2924. addstatement(newstatement,ctemprefnode.create(temp));
  2925. left:=nil;
  2926. end;
  2927. end;
  2928. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2929. begin
  2930. first_ansistring_to_pchar:=nil;
  2931. expectloc:=LOC_REGISTER;
  2932. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2933. if (cs_create_pic in current_settings.moduleswitches) then
  2934. include(current_procinfo.flags,pi_needs_got);
  2935. end;
  2936. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2937. begin
  2938. first_arrayconstructor_to_set:=nil;
  2939. internalerror(200104022);
  2940. end;
  2941. function ttypeconvnode.first_class_to_intf : tnode;
  2942. var
  2943. hd : tobjectdef;
  2944. ImplIntf : TImplementedInterface;
  2945. begin
  2946. result:=nil;
  2947. expectloc:=LOC_REGISTER;
  2948. hd:=tobjectdef(left.resultdef);
  2949. while assigned(hd) do
  2950. begin
  2951. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  2952. if assigned(ImplIntf) then
  2953. begin
  2954. case ImplIntf.IType of
  2955. etStandard:
  2956. { handle in pass 2 }
  2957. ;
  2958. etFieldValue, etFieldValueClass:
  2959. if is_interface(tobjectdef(resultdef)) then
  2960. begin
  2961. result:=left;
  2962. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2963. { this ensures proper refcounting when field is of class type }
  2964. if not is_interface(result.resultdef) then
  2965. inserttypeconv(result, resultdef);
  2966. left:=nil;
  2967. end
  2968. else
  2969. begin
  2970. internalerror(200802213);
  2971. end;
  2972. etStaticMethodResult, etStaticMethodClass,
  2973. etVirtualMethodResult, etVirtualMethodClass:
  2974. if is_interface(tobjectdef(resultdef)) then
  2975. begin
  2976. { TODO: generating a call to TObject.GetInterface instead could yield
  2977. smaller code size. OTOH, refcounting gotchas are possible that way. }
  2978. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2979. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2980. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2981. left,[]);
  2982. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2983. { if it is a class, process it further in a similar way }
  2984. if not is_interface(result.resultdef) then
  2985. inserttypeconv(result, resultdef);
  2986. left:=nil;
  2987. end
  2988. else if is_class(tobjectdef(resultdef)) then
  2989. begin
  2990. internalerror(200802211);
  2991. end
  2992. else
  2993. internalerror(200802231);
  2994. else
  2995. internalerror(200802165);
  2996. end;
  2997. break;
  2998. end;
  2999. hd:=hd.childof;
  3000. end;
  3001. if hd=nil then
  3002. internalerror(200802164);
  3003. end;
  3004. function ttypeconvnode.first_string_to_string : tnode;
  3005. var
  3006. procname: string[31];
  3007. newblock : tblocknode;
  3008. newstat : tstatementnode;
  3009. restemp : ttempcreatenode;
  3010. begin
  3011. { get the correct procedure name }
  3012. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3013. '_to_'+tstringdef(resultdef).stringtypname;
  3014. if tstringdef(resultdef).stringtype=st_shortstring then
  3015. begin
  3016. newblock:=internalstatements(newstat);
  3017. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3018. addstatement(newstat,restemp);
  3019. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3020. ctemprefnode.create(restemp),nil))));
  3021. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  3022. addstatement(newstat,ctemprefnode.create(restemp));
  3023. result:=newblock;
  3024. end
  3025. { encoding parameter required? }
  3026. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  3027. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  3028. result:=ccallnode.createinternres(procname,
  3029. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  3030. ccallparanode.create(left,nil)),resultdef)
  3031. else
  3032. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  3033. left:=nil;
  3034. end;
  3035. function ttypeconvnode._first_int_to_int : tnode;
  3036. begin
  3037. result:=first_int_to_int;
  3038. end;
  3039. function ttypeconvnode._first_cstring_to_pchar : tnode;
  3040. begin
  3041. result:=first_cstring_to_pchar;
  3042. end;
  3043. function ttypeconvnode._first_cstring_to_int : tnode;
  3044. begin
  3045. result:=first_cstring_to_int;
  3046. end;
  3047. function ttypeconvnode._first_string_to_chararray : tnode;
  3048. begin
  3049. result:=first_string_to_chararray;
  3050. end;
  3051. function ttypeconvnode._first_char_to_string : tnode;
  3052. begin
  3053. result:=first_char_to_string;
  3054. end;
  3055. function ttypeconvnode._first_char_to_chararray: tnode;
  3056. begin
  3057. result:=first_char_to_chararray;
  3058. end;
  3059. function ttypeconvnode._first_nothing : tnode;
  3060. begin
  3061. result:=first_nothing;
  3062. end;
  3063. function ttypeconvnode._first_array_to_pointer : tnode;
  3064. begin
  3065. result:=first_array_to_pointer;
  3066. end;
  3067. function ttypeconvnode._first_int_to_real : tnode;
  3068. begin
  3069. result:=first_int_to_real;
  3070. end;
  3071. function ttypeconvnode._first_real_to_real : tnode;
  3072. begin
  3073. result:=first_real_to_real;
  3074. end;
  3075. function ttypeconvnode._first_pointer_to_array : tnode;
  3076. begin
  3077. result:=first_pointer_to_array;
  3078. end;
  3079. function ttypeconvnode._first_cchar_to_pchar : tnode;
  3080. begin
  3081. result:=first_cchar_to_pchar;
  3082. end;
  3083. function ttypeconvnode._first_bool_to_int : tnode;
  3084. begin
  3085. result:=first_bool_to_int;
  3086. end;
  3087. function ttypeconvnode._first_int_to_bool : tnode;
  3088. begin
  3089. result:=first_int_to_bool;
  3090. end;
  3091. function ttypeconvnode._first_bool_to_bool : tnode;
  3092. begin
  3093. result:=first_bool_to_bool;
  3094. end;
  3095. function ttypeconvnode._first_proc_to_procvar : tnode;
  3096. begin
  3097. result:=first_proc_to_procvar;
  3098. end;
  3099. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  3100. begin
  3101. result:=first_nil_to_methodprocvar;
  3102. end;
  3103. function ttypeconvnode._first_set_to_set : tnode;
  3104. begin
  3105. result:=first_set_to_set;
  3106. end;
  3107. function ttypeconvnode._first_cord_to_pointer : tnode;
  3108. begin
  3109. result:=first_cord_to_pointer;
  3110. end;
  3111. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  3112. begin
  3113. result:=first_ansistring_to_pchar;
  3114. end;
  3115. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  3116. begin
  3117. result:=first_arrayconstructor_to_set;
  3118. end;
  3119. function ttypeconvnode._first_class_to_intf : tnode;
  3120. begin
  3121. result:=first_class_to_intf;
  3122. end;
  3123. function ttypeconvnode._first_char_to_char : tnode;
  3124. begin
  3125. result:=first_char_to_char;
  3126. end;
  3127. function ttypeconvnode._first_string_to_string : tnode;
  3128. begin
  3129. result:=first_string_to_string;
  3130. end;
  3131. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  3132. const
  3133. firstconvert : array[tconverttype] of pointer = (
  3134. nil, { none }
  3135. @ttypeconvnode._first_nothing, {equal}
  3136. @ttypeconvnode._first_nothing, {not_possible}
  3137. @ttypeconvnode._first_string_to_string,
  3138. @ttypeconvnode._first_char_to_string,
  3139. @ttypeconvnode._first_char_to_chararray,
  3140. nil, { removed in typecheck_chararray_to_string }
  3141. @ttypeconvnode._first_cchar_to_pchar,
  3142. @ttypeconvnode._first_cstring_to_pchar,
  3143. @ttypeconvnode._first_cstring_to_int,
  3144. @ttypeconvnode._first_ansistring_to_pchar,
  3145. @ttypeconvnode._first_string_to_chararray,
  3146. nil, { removed in typecheck_chararray_to_string }
  3147. @ttypeconvnode._first_array_to_pointer,
  3148. @ttypeconvnode._first_pointer_to_array,
  3149. @ttypeconvnode._first_int_to_int,
  3150. @ttypeconvnode._first_int_to_bool,
  3151. @ttypeconvnode._first_bool_to_bool,
  3152. @ttypeconvnode._first_bool_to_int,
  3153. @ttypeconvnode._first_real_to_real,
  3154. @ttypeconvnode._first_int_to_real,
  3155. nil, { removed in typecheck_real_to_currency }
  3156. @ttypeconvnode._first_proc_to_procvar,
  3157. @ttypeconvnode._first_nil_to_methodprocvar,
  3158. @ttypeconvnode._first_arrayconstructor_to_set,
  3159. @ttypeconvnode._first_set_to_set,
  3160. @ttypeconvnode._first_cord_to_pointer,
  3161. @ttypeconvnode._first_nothing,
  3162. @ttypeconvnode._first_nothing,
  3163. @ttypeconvnode._first_class_to_intf,
  3164. @ttypeconvnode._first_char_to_char,
  3165. @ttypeconvnode._first_nothing,
  3166. @ttypeconvnode._first_nothing,
  3167. nil,
  3168. nil,
  3169. nil,
  3170. nil,
  3171. nil,
  3172. nil,
  3173. nil,
  3174. @ttypeconvnode._first_nothing
  3175. );
  3176. type
  3177. tprocedureofobject = function : tnode of object;
  3178. var
  3179. r : packed record
  3180. proc : pointer;
  3181. obj : pointer;
  3182. end;
  3183. begin
  3184. { this is a little bit dirty but it works }
  3185. { and should be quite portable too }
  3186. r.proc:=firstconvert[c];
  3187. r.obj:=self;
  3188. if not assigned(r.proc) then
  3189. internalerror(200312081);
  3190. first_call_helper:=tprocedureofobject(r)()
  3191. end;
  3192. function ttypeconvnode.pass_1 : tnode;
  3193. begin
  3194. if warn_pointer_to_signed then
  3195. cgmessage(type_w_pointer_to_signed);
  3196. result:=nil;
  3197. firstpass(left);
  3198. if codegenerror then
  3199. exit;
  3200. expectloc:=left.expectloc;
  3201. if nf_explicit in flags then
  3202. { check if the result could be in a register }
  3203. if (not(tstoreddef(resultdef).is_intregable) and
  3204. not(tstoreddef(resultdef).is_fpuregable)) or
  3205. ((left.resultdef.typ = floatdef) and
  3206. (resultdef.typ <> floatdef)) then
  3207. make_not_regable(left,[ra_addr_regable]);
  3208. result:=first_call_helper(convtype);
  3209. end;
  3210. function ttypeconvnode.retains_value_location:boolean;
  3211. begin
  3212. result:=(convtype=tc_equal) or
  3213. { typecasting from void is always allowed }
  3214. is_void(left.resultdef) or
  3215. (left.resultdef.typ=formaldef) or
  3216. { int 2 int with same size reuses same location, or for
  3217. tp7 mode also allow size < orignal size }
  3218. (
  3219. (convtype=tc_int_2_int) and
  3220. (
  3221. not is_bitpacked_access(left) and
  3222. (resultdef.size=left.resultdef.size) or
  3223. ((m_tp7 in current_settings.modeswitches) and
  3224. (resultdef.size<left.resultdef.size))
  3225. )
  3226. ) or
  3227. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  3228. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  3229. (nf_explicit in flags) and
  3230. (resultdef.size=left.resultdef.size)) or
  3231. { on managed platforms, converting an element to an open array
  3232. involves creating an actual array -> value location changes }
  3233. ((convtype=tc_elem_2_openarray) and
  3234. not(target_info.system in systems_managed_vm));
  3235. end;
  3236. function ttypeconvnode.assign_allowed:boolean;
  3237. begin
  3238. result:=retains_value_location;
  3239. { When using only a part of the value it can't be in a register since
  3240. that will load the value in a new register first }
  3241. { the same goes for changing the sign of equal-sized values which
  3242. are smaller than an entire register }
  3243. if result and
  3244. { don't try to check the size of an open array }
  3245. (is_open_array(resultdef) or
  3246. (resultdef.size<left.resultdef.size) or
  3247. ((resultdef.size=left.resultdef.size) and
  3248. (left.resultdef.size<sizeof(aint)) and
  3249. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  3250. make_not_regable(left,[ra_addr_regable]);
  3251. end;
  3252. function ttypeconvnode.docompare(p: tnode) : boolean;
  3253. begin
  3254. docompare :=
  3255. inherited docompare(p) and
  3256. (convtype = ttypeconvnode(p).convtype) and
  3257. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  3258. end;
  3259. procedure ttypeconvnode._second_int_to_int;
  3260. begin
  3261. second_int_to_int;
  3262. end;
  3263. procedure ttypeconvnode._second_string_to_string;
  3264. begin
  3265. second_string_to_string;
  3266. end;
  3267. procedure ttypeconvnode._second_cstring_to_pchar;
  3268. begin
  3269. second_cstring_to_pchar;
  3270. end;
  3271. procedure ttypeconvnode._second_cstring_to_int;
  3272. begin
  3273. second_cstring_to_int;
  3274. end;
  3275. procedure ttypeconvnode._second_string_to_chararray;
  3276. begin
  3277. second_string_to_chararray;
  3278. end;
  3279. procedure ttypeconvnode._second_array_to_pointer;
  3280. begin
  3281. second_array_to_pointer;
  3282. end;
  3283. procedure ttypeconvnode._second_pointer_to_array;
  3284. begin
  3285. second_pointer_to_array;
  3286. end;
  3287. procedure ttypeconvnode._second_chararray_to_string;
  3288. begin
  3289. second_chararray_to_string;
  3290. end;
  3291. procedure ttypeconvnode._second_char_to_string;
  3292. begin
  3293. second_char_to_string;
  3294. end;
  3295. procedure ttypeconvnode._second_int_to_real;
  3296. begin
  3297. second_int_to_real;
  3298. end;
  3299. procedure ttypeconvnode._second_real_to_real;
  3300. begin
  3301. second_real_to_real;
  3302. end;
  3303. procedure ttypeconvnode._second_cord_to_pointer;
  3304. begin
  3305. second_cord_to_pointer;
  3306. end;
  3307. procedure ttypeconvnode._second_proc_to_procvar;
  3308. begin
  3309. second_proc_to_procvar;
  3310. end;
  3311. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3312. begin
  3313. second_nil_to_methodprocvar;
  3314. end;
  3315. procedure ttypeconvnode._second_bool_to_int;
  3316. begin
  3317. second_bool_to_int;
  3318. end;
  3319. procedure ttypeconvnode._second_int_to_bool;
  3320. begin
  3321. second_int_to_bool;
  3322. end;
  3323. procedure ttypeconvnode._second_bool_to_bool;
  3324. begin
  3325. second_bool_to_bool;
  3326. end;
  3327. procedure ttypeconvnode._second_set_to_set;
  3328. begin
  3329. second_set_to_set;
  3330. end;
  3331. procedure ttypeconvnode._second_ansistring_to_pchar;
  3332. begin
  3333. second_ansistring_to_pchar;
  3334. end;
  3335. procedure ttypeconvnode._second_class_to_intf;
  3336. begin
  3337. second_class_to_intf;
  3338. end;
  3339. procedure ttypeconvnode._second_char_to_char;
  3340. begin
  3341. second_char_to_char;
  3342. end;
  3343. procedure ttypeconvnode._second_elem_to_openarray;
  3344. begin
  3345. second_elem_to_openarray;
  3346. end;
  3347. procedure ttypeconvnode._second_nothing;
  3348. begin
  3349. second_nothing;
  3350. end;
  3351. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3352. const
  3353. secondconvert : array[tconverttype] of pointer = (
  3354. @ttypeconvnode._second_nothing, {none}
  3355. @ttypeconvnode._second_nothing, {equal}
  3356. @ttypeconvnode._second_nothing, {not_possible}
  3357. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3358. @ttypeconvnode._second_char_to_string,
  3359. @ttypeconvnode._second_nothing, {char_to_charray}
  3360. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3361. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3362. @ttypeconvnode._second_cstring_to_pchar,
  3363. @ttypeconvnode._second_cstring_to_int,
  3364. @ttypeconvnode._second_ansistring_to_pchar,
  3365. @ttypeconvnode._second_string_to_chararray,
  3366. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3367. @ttypeconvnode._second_array_to_pointer,
  3368. @ttypeconvnode._second_pointer_to_array,
  3369. @ttypeconvnode._second_int_to_int,
  3370. @ttypeconvnode._second_int_to_bool,
  3371. @ttypeconvnode._second_bool_to_bool,
  3372. @ttypeconvnode._second_bool_to_int,
  3373. @ttypeconvnode._second_real_to_real,
  3374. @ttypeconvnode._second_int_to_real,
  3375. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3376. @ttypeconvnode._second_proc_to_procvar,
  3377. @ttypeconvnode._second_nil_to_methodprocvar,
  3378. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3379. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3380. @ttypeconvnode._second_cord_to_pointer,
  3381. @ttypeconvnode._second_nothing, { interface 2 string }
  3382. @ttypeconvnode._second_nothing, { interface 2 guid }
  3383. @ttypeconvnode._second_class_to_intf,
  3384. @ttypeconvnode._second_char_to_char,
  3385. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3386. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3387. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3388. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3389. @ttypeconvnode._second_nothing, { variant_2_enum }
  3390. @ttypeconvnode._second_nothing, { enum_2_variant }
  3391. @ttypeconvnode._second_nothing, { variant_2_interface }
  3392. @ttypeconvnode._second_nothing, { interface_2_variant }
  3393. @ttypeconvnode._second_nothing, { array_2_dynarray }
  3394. @ttypeconvnode._second_elem_to_openarray { elem_2_openarray }
  3395. );
  3396. type
  3397. tprocedureofobject = procedure of object;
  3398. var
  3399. r : packed record
  3400. proc : pointer;
  3401. obj : pointer;
  3402. end;
  3403. begin
  3404. { this is a little bit dirty but it works }
  3405. { and should be quite portable too }
  3406. r.proc:=secondconvert[c];
  3407. r.obj:=self;
  3408. tprocedureofobject(r)();
  3409. end;
  3410. {*****************************************************************************
  3411. TASNODE
  3412. *****************************************************************************}
  3413. function tasisnode.target_specific_typecheck: boolean;
  3414. begin
  3415. result:=false;
  3416. end;
  3417. function tasisnode.pass_typecheck: tnode;
  3418. var
  3419. hp : tnode;
  3420. begin
  3421. result:=nil;
  3422. typecheckpass(right);
  3423. typecheckpass(left);
  3424. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3425. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3426. if codegenerror then
  3427. exit;
  3428. if target_specific_typecheck then
  3429. begin
  3430. // ok
  3431. end
  3432. else if (right.resultdef.typ=classrefdef) then
  3433. begin
  3434. { left maybe an interface reference }
  3435. if is_interfacecom(left.resultdef) or
  3436. is_javainterface(left.resultdef) then
  3437. begin
  3438. { relation checks are not possible }
  3439. end
  3440. { or left must be a class }
  3441. else if is_class(left.resultdef) or
  3442. is_javaclass(left.resultdef) then
  3443. begin
  3444. { the operands must be related }
  3445. if (not(def_is_related(tobjectdef(left.resultdef),
  3446. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3447. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  3448. tobjectdef(left.resultdef)))) then
  3449. CGMessage2(type_e_classes_not_related,
  3450. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3451. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3452. end
  3453. else
  3454. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3455. case nodetype of
  3456. isn:
  3457. resultdef:=pasbool8type;
  3458. asn:
  3459. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3460. end;
  3461. end
  3462. else if is_interface(right.resultdef) or
  3463. is_dispinterface(right.resultdef) or
  3464. is_javainterface(right.resultdef) then
  3465. begin
  3466. case nodetype of
  3467. isn:
  3468. resultdef:=pasbool8type;
  3469. asn:
  3470. resultdef:=right.resultdef;
  3471. end;
  3472. { left is a class or interface }
  3473. if is_javainterface(right.resultdef) then
  3474. begin
  3475. if not is_java_class_or_interface(left.resultdef) then
  3476. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3477. end
  3478. else if not(is_class(left.resultdef) or
  3479. is_interfacecom(left.resultdef)) then
  3480. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  3481. else
  3482. begin
  3483. { load the GUID of the interface }
  3484. if (right.nodetype=typen) then
  3485. begin
  3486. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3487. begin
  3488. if assigned(tobjectdef(right.resultdef).iidstr) then
  3489. begin
  3490. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3491. tstringconstnode(hp).changestringtype(cshortstringtype);
  3492. right.free;
  3493. right:=hp;
  3494. end
  3495. else
  3496. internalerror(201006131);
  3497. end
  3498. else
  3499. begin
  3500. if assigned(tobjectdef(right.resultdef).iidguid) then
  3501. begin
  3502. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3503. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3504. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3505. right.free;
  3506. right:=hp;
  3507. end
  3508. else
  3509. internalerror(201006132);
  3510. end;
  3511. typecheckpass(right);
  3512. end;
  3513. end;
  3514. end
  3515. else
  3516. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3517. end;
  3518. {*****************************************************************************
  3519. TISNODE
  3520. *****************************************************************************}
  3521. constructor tisnode.create(l,r : tnode);
  3522. begin
  3523. inherited create(isn,l,r);
  3524. end;
  3525. constructor tisnode.create_internal(l, r: tnode);
  3526. begin
  3527. create(l,r);
  3528. include(flags,nf_internal);
  3529. end;
  3530. function tisnode.pass_1 : tnode;
  3531. var
  3532. procname: string;
  3533. begin
  3534. result:=nil;
  3535. { Passing a class type to an "is" expression cannot result in a class
  3536. of that type to be constructed.
  3537. }
  3538. include(right.flags,nf_ignore_for_wpo);
  3539. if is_class(left.resultdef) and
  3540. (right.resultdef.typ=classrefdef) then
  3541. result := ccallnode.createinternres('fpc_do_is',
  3542. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3543. resultdef)
  3544. else
  3545. begin
  3546. if is_class(left.resultdef) then
  3547. if is_shortstring(right.resultdef) then
  3548. procname := 'fpc_class_is_corbaintf'
  3549. else
  3550. procname := 'fpc_class_is_intf'
  3551. else
  3552. if right.resultdef.typ=classrefdef then
  3553. procname := 'fpc_intf_is_class'
  3554. else
  3555. procname := 'fpc_intf_is';
  3556. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3557. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3558. end;
  3559. left := nil;
  3560. right := nil;
  3561. //firstpass(call);
  3562. if codegenerror then
  3563. exit;
  3564. end;
  3565. { dummy pass_2, it will never be called, but we need one since }
  3566. { you can't instantiate an abstract class }
  3567. procedure tisnode.pass_generate_code;
  3568. begin
  3569. end;
  3570. {*****************************************************************************
  3571. TASNODE
  3572. *****************************************************************************}
  3573. constructor tasnode.create(l,r : tnode);
  3574. begin
  3575. inherited create(asn,l,r);
  3576. call := nil;
  3577. end;
  3578. constructor tasnode.create_internal(l,r : tnode);
  3579. begin
  3580. create(l,r);
  3581. include(flags,nf_internal);
  3582. end;
  3583. destructor tasnode.destroy;
  3584. begin
  3585. call.free;
  3586. inherited destroy;
  3587. end;
  3588. function tasnode.dogetcopy: tnode;
  3589. begin
  3590. result := inherited dogetcopy;
  3591. if assigned(call) then
  3592. tasnode(result).call := call.getcopy
  3593. else
  3594. tasnode(result).call := nil;
  3595. end;
  3596. function tasnode.docompare(p: tnode): boolean;
  3597. begin
  3598. result:=
  3599. inherited docompare(p) and
  3600. tasnode(p).call.isequal(call);
  3601. end;
  3602. function tasnode.pass_1 : tnode;
  3603. var
  3604. procname: string;
  3605. begin
  3606. result:=nil;
  3607. { Passing a class type to an "as" expression cannot result in a class
  3608. of that type to be constructed.
  3609. }
  3610. include(right.flags,nf_ignore_for_wpo);
  3611. if not assigned(call) then
  3612. begin
  3613. if is_class(left.resultdef) and
  3614. (right.resultdef.typ=classrefdef) then
  3615. call := ccallnode.createinternres('fpc_do_as',
  3616. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3617. resultdef)
  3618. else
  3619. begin
  3620. if is_class(left.resultdef) then
  3621. if is_shortstring(right.resultdef) then
  3622. procname := 'fpc_class_as_corbaintf'
  3623. else
  3624. procname := 'fpc_class_as_intf'
  3625. else
  3626. if right.resultdef.typ=classrefdef then
  3627. procname := 'fpc_intf_as_class'
  3628. else
  3629. procname := 'fpc_intf_as';
  3630. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3631. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3632. end;
  3633. left := nil;
  3634. right := nil;
  3635. firstpass(call);
  3636. if codegenerror then
  3637. exit;
  3638. expectloc:=call.expectloc;
  3639. end;
  3640. end;
  3641. end.