ncnv.pas 173 KB

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