ncnv.pas 154 KB

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