ncnv.pas 151 KB

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