ncnv.pas 194 KB

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