ncnv.pas 202 KB

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