ncnv.pas 175 KB

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