ncnv.pas 159 KB

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