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