ncnv.pas 176 KB

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