ncnv.pas 172 KB

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