ncnv.pas 202 KB

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