ncnv.pas 155 KB

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