ncnv.pas 158 KB

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