ncnv.pas 174 KB

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