ncnv.pas 185 KB

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