ncnv.pas 154 KB

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