ncnv.pas 173 KB

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