ncnv.pas 175 KB

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